Home

Example Application: Percentage Conversion

   

Introduction

This application shows different ways to convert a decimal to a percentage value. The values include regular decimals and fractions. Another part of the exercise shows how to convert a percentage value to a decimal.

Practical LearningPractical Learning: Creating the Application

  1. To create a new application, on the main menu, click File -> New Project...
  2. In the middle list, click Windows Forms Application
  3. Change the Name to PercentageConversions1
  4. Click OK
  5. Design the form as follows: 
     
    Percentation Conversion
     
    Control Text Name ReadOnly TextAlign
    GroupBox GroupBox Converting a Percentage to Decimal      
    Label Label V&alue:      
    TextBox TextBox 5 txtPercentage1   Right
    Label Label % of      
    TextBox TextBox 10 txtValue1   Right
    Button Button = btnConvert1    
    TextBox TextBox   txtValue1 True Right
    GroupBox GroupBox Converting a Fraction to a Percentage      
    TextBox TextBox 3 txtNumerator   Right
    Label Label /      
    TextBox TextBox 4 txtDenominator   Right
    Button Button = btnFractionToPercent    
    TextBox TextBox   txtPercentage2 True Right
    Label Label %      
    GroupBox GroupBox Converting a Decimal to Percentage      
    Label Label Val&ue:      
    TextBox TextBox 0.125 txtDecimal1   Right
    Button Button = btnDecimalToPercent    
    TextBox TextBox   txtPercentage3 True Right
    Label Label %      
    GroupBox GroupBox Converting a Percentage to Decimal      
    Label Label Valu&e:      
    TextBox TextBox 75 txtPercentage4   Right
    Label Label %      
    Button Button = btnPercentToDecimal    
    TextBox TextBox   txtDecimal2 True Right
    Button Button Close btnClose    
  6. Double-click the first = button
  7. Implement its event as follows:
    System::Void btnConvert1_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	double percent = 0.00;
        double value = 0.00;
        double result;
    
        try
        {
            percent = double::Parse(txtPercentage1->Text);
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Percent Value", L"Percentage Conversion");
        }
    
        try
        {
            value = double::Parse(txtValue1->Text);
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Value to Convert", L"Percentage Conversion");
        }
    
        result = percent * value / 100;
        txtResult1->Text = result.ToString(L"F");
    }
  8. Return to the form
  9. Double-click the second = button
  10. Implement its event as follows:
    System::Void btnFractionToPercent_Click(System::Object^  sender,
    		   			System::EventArgs^  e)
    {
        double numerator = 0.00;
        double denominator = 0.00;
        double result;
    
        try
        {
            numerator = double::Parse(txtNumerator->Text);
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Numerator", L"Percentage Conversion");
        }
    
        try
        {
            denominator = double::Parse(txtDenominator->Text);
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Value to Convert",
            		 L"Percentage Conversion");
        }
    
        result = (numerator / denominator) * 100;
        txtPercentage2->Text = result.ToString(L"F");
    }
  11. Return to the form
  12. Double-click the third = button
  13. Implement its event as follows:
    System::Void btnDecimalToPercent_Click(System::Object^  sender,
    		  	System::EventArgs^  e)
    {
        double value = 0.00;
        double result;
    
        try
        {
            value = double::Parse(txtDecimal1->Text);
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Value to Convert",
            		 L"Percentage Conversion");
        }
    
        result = value * 100;
        txtPercentage3->Text = result.ToString(L"F");
    }
  14. Return to the form
  15. Double-click the last = button
  16. Implement its event as follows:
    System::Void btnPercentToDecimal_Click(System::Object^  sender,
    		  	 System::EventArgs^  e)
    {
        double value = 0.00;
        double result;
    
        try
        {
            value = double::Parse(txtPercentage4->Text);
        }
        catch(FormatException ^)
        {
            MessageBox::Show(L"Invalid Value to Convert",
            		 L"Percentage Conversion");
        }
    
        result = value / 100;
        txtDecimal2->Text = result.ToString(L"F");
    }
  17. Return to the form
  18. Double-click the Close button
  19. Implement its event as follows:
    System::Void btnClose_Click(System::Object^  sender, System::EventArgs^  e)
    {
        Close();
    }
  20. To execute, press F5
  21. Enter some values and click the = buttons
     
    Percentation Conversion
  22. Close the form and return to the your programming environment
 
 
     
 

Home Copyright © 2011 FunctionX, Inc.