![Home](../../design/logo1.gif) |
Example Application: Percentage Conversion |
|
|
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
Learning: Creating the Application
|
|
- To create a new application, on the main menu, click File -> New
Project...
- In the middle list, click Windows Forms Application
- Change the Name to PercentageConversions1
- Click OK
- Design the form as follows:
![Percentation Conversion](../../vs2010/forms/percentages5.gif) |
Control |
Text |
Name |
ReadOnly |
TextAlign |
GroupBox |
![GroupBox](../../vs2010/controls/groupbox1.gif) |
Converting a Percentage to Decimal |
|
|
|
Label |
![Label](../../vs2010/controls/label1.gif) |
V&alue: |
|
|
|
TextBox |
![TextBox](../../vs2010/controls/textbox1.gif) |
5 |
txtPercentage1 |
|
Right |
Label |
![Label](../../vs2010/controls/label1.gif) |
% of |
|
|
|
TextBox |
![TextBox](../../vs2010/controls/textbox1.gif) |
10 |
txtValue1 |
|
Right |
Button |
![Button](../../vs2010/controls/button1.gif) |
= |
btnConvert1 |
|
|
TextBox |
![TextBox](../../vs2010/controls/textbox1.gif) |
|
txtValue1 |
True |
Right |
GroupBox |
![GroupBox](../../vs2010/controls/groupbox1.gif) |
Converting a Fraction to a Percentage |
|
|
|
TextBox |
![TextBox](../../vs2010/controls/textbox1.gif) |
3 |
txtNumerator |
|
Right |
Label |
![Label](../../vs2010/controls/label1.gif) |
/ |
|
|
|
TextBox |
![TextBox](../../vs2010/controls/textbox1.gif) |
4 |
txtDenominator |
|
Right |
Button |
![Button](../../vs2010/controls/button1.gif) |
= |
btnFractionToPercent |
|
|
TextBox |
![TextBox](../../vs2010/controls/textbox1.gif) |
|
txtPercentage2 |
True |
Right |
Label |
![Label](../../vs2010/controls/label1.gif) |
% |
|
|
|
GroupBox |
![GroupBox](../../vs2010/controls/groupbox1.gif) |
Converting a Decimal to Percentage |
|
|
|
Label |
![Label](../../vs2010/controls/label1.gif) |
Val&ue: |
|
|
|
TextBox |
![TextBox](../../vs2010/controls/textbox1.gif) |
0.125 |
txtDecimal1 |
|
Right |
Button |
![Button](../../vs2010/controls/button1.gif) |
= |
btnDecimalToPercent |
|
|
TextBox |
![TextBox](../../vs2010/controls/textbox1.gif) |
|
txtPercentage3 |
True |
Right |
Label |
![Label](../../vs2010/controls/label1.gif) |
% |
|
|
|
GroupBox |
![GroupBox](../../vs2010/controls/groupbox1.gif) |
Converting a Percentage to Decimal |
|
|
|
Label |
![Label](../../vs2010/controls/label1.gif) |
Valu&e: |
|
|
|
TextBox |
![TextBox](../../vs2010/controls/textbox1.gif) |
75 |
txtPercentage4 |
|
Right |
Label |
![Label](../../vs2010/controls/label1.gif) |
% |
|
|
|
Button |
![Button](../../vs2010/controls/button1.gif) |
= |
btnPercentToDecimal |
|
|
TextBox |
![TextBox](../../vs2010/controls/textbox1.gif) |
|
txtDecimal2 |
True |
Right |
Button |
![Button](../../vs2010/controls/button1.gif) |
Close |
btnClose |
|
|
|
- Double-click the first = button
- 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");
}
- Return to the form
- Double-click the second = button
- 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");
}
- Return to the form
- Double-click the third = button
- 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");
}
- Return to the form
- Double-click the last = button
- 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");
}
- Return to the form
- Double-click the Close button
- Implement its event as follows:
System::Void btnClose_Click(System::Object^ sender, System::EventArgs^ e)
{
Close();
}
- To execute, press F5
- Enter some values and click the = buttons
![Percentation Conversion](../../vs2010/forms/percentages7.gif)
- Close the form and return to the your programming environment
|
|