|
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:
|
Control |
Text |
Name |
ReadOnly |
TextAlign |
GroupBox |
|
Converting a Percentage to Decimal |
|
|
|
Label |
|
V&alue: |
|
|
|
TextBox |
|
5 |
txtPercentage1 |
|
Right |
Label |
|
% of |
|
|
|
TextBox |
|
10 |
txtValue1 |
|
Right |
Button |
|
= |
btnConvert1 |
|
|
TextBox |
|
|
txtValue1 |
True |
Right |
GroupBox |
|
Converting a Fraction to a Percentage |
|
|
|
TextBox |
|
3 |
txtNumerator |
|
Right |
Label |
|
/ |
|
|
|
TextBox |
|
4 |
txtDenominator |
|
Right |
Button |
|
= |
btnFractionToPercent |
|
|
TextBox |
|
|
txtPercentage2 |
True |
Right |
Label |
|
% |
|
|
|
GroupBox |
|
Converting a Decimal to Percentage |
|
|
|
Label |
|
Val&ue: |
|
|
|
TextBox |
|
0.125 |
txtDecimal1 |
|
Right |
Button |
|
= |
btnDecimalToPercent |
|
|
TextBox |
|
|
txtPercentage3 |
True |
Right |
Label |
|
% |
|
|
|
GroupBox |
|
Converting a Percentage to Decimal |
|
|
|
Label |
|
Valu&e: |
|
|
|
TextBox |
|
75 |
txtPercentage4 |
|
Right |
Label |
|
% |
|
|
|
Button |
|
= |
btnPercentToDecimal |
|
|
TextBox |
|
|
txtDecimal2 |
True |
Right |
Button |
|
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
- Close the form and return to the your programming environment
|
|