This exercise combines labels, panels, radio buttons, and
text boxes to create an application. It illustrates how to use radio
buttons as regular buttons
Prerequisites:
Panel
Button
Scroll Bars |
|
Practical
Learning: Using Radio Buttons |
|
- Start Microsoft Visual Studio 2005
- On the Start Page, click Project button on the right side of Create
(alternatively, on the main
menu, you can click File -> New -> Project...)
- In the Templates list, click Windows Forms Application
- In the Name edit box, replace the <Enter name> content with Calculate1
- Click OK
- Design the form as follows:
|
Control |
Name |
Text |
Other Properties |
GroupBox |
|
|
Values |
|
Label |
|
|
Number 1: |
|
TextBox |
|
txtNumber1 |
0.00 |
TextAlign: Right |
Label |
|
|
Number 2: |
|
TextBox |
|
txtNumber2 |
0.00 |
TextAlign: Right |
Label |
|
|
Result: |
|
TextBox |
|
txtResult |
0.00 |
TextAlign: Right |
GroupBox |
|
|
Operation |
|
RadioButton |
|
rdoAddition |
Addition |
|
RadioButton |
|
rdoSubtraction |
Subtraction |
|
RadioButton |
|
rdoMultiplication |
Multiplication |
|
RadioButton |
|
rdoDivision |
Division |
|
|
- Double-click the Addition radio button and implement its event as
follows:
System::Void rdoAddition_CheckedChanged(System::Object^ sender,
System::EventArgs^ e)
{
double Number1, Number2, Result;
try {
Number1 = Convert::ToDouble(txtNumber1->Text);
Number2 = Convert::ToDouble(txtNumber2->Text);
Result = Number1 + Number2;
txtResult->Text = Result.ToString();
}
catch(System::FormatException *fmtE)
{
MessageBox::Show(fmtE->Message);
}
}
|
- Display the form
- Double-click the Subtraction radio button and implement its event as
follows:
System::Void rdoSubtraction_CheckedChanged(System::Object^ sender,
System::EventArgs^ e)
{
double Number1, Number2, Result;
try {
Number1 = Convert::ToDouble(txtNumber1->Text);
Number2 = Convert::ToDouble(txtNumber2->Text);
Result = Number1 - Number2;
txtResult->Text = Result.ToString();
}
catch(System::FormatException ^ e)
{
MessageBox::Show(e->Message);
}
}
|
- Return to the form
- Double-click the Multiplication radio button and implement its event
as follows:
System::Void rdoMultiplication_CheckedChanged(System::Object^ sender,
System::EventArgs^ e)
{
double Number1, Number2, Result;
try {
Number1 = Convert::ToDouble(txtNumber1->Text);
Number2 = Convert::ToDouble(txtNumber2->Text);
Result = Number1 * Number2;
txtResult->Text = Result.ToString();
}
catch(System::FormatException ^ e)
{
MessageBox::Show(e->Message);
}
}
|
- Return to the form
- Double-click the Division radio button and implement its event as
follows:
System::Void rdoDivision_CheckedChanged(System::Object^ sender,
System::EventArgs^ e)
{
double Number1, Number2, Result;
try {
Number1 = Convert::ToDouble(txtNumber1->Text);
Number2 = Convert::ToDouble(txtNumber2->Text);
Result = Number1 / Number2;
txtResult->Text = Result.ToString();
}
catch(System::FormatException ^ e)
{
MessageBox::Show(e->Message);
}
}
|
- Test the application
|
|