Home

Microsoft Visual C# Example Application: Percentage Conversions

     

Introduction

In mathematics and related subjects such as algebra or statistics, a percentage is used to express a value that is a fraction between 0 and 1. It is sometimes expressed as x%.

There are various scenarios for which you deal with percent values:

  • Sometimes you get a decimal value that is between 0 and 1, such as 0.86 or 0.1245, and you need its percentage equivalent
  • Sometimes you are given a fraction, such as 3/8 and must know how must that value represents in percent
  • Sometimes you have a percent value but must convert it to decimal

ApplicationApplication: Creating the Application

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

Application

 

Home Copyright © 2010-2011 FunctionX