Example Applications: Calculation

 

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

Using Radio Buttons

Practical LearningPractical Learning: Using Radio Buttons

  1. Start Microsoft Visual Studio 2005
  2. On the Start Page, click Project button on the right side of Create (alternatively, on the main menu, you can click File -> New -> Project...)
  3. In the Templates list, click Windows Forms Application
  4. In the Name edit box, replace the <Enter name> content with Calculate1
  5. Click OK
  6. Design the form as follows:
     
     
    Control Name Text Other Properties
    GroupBox GroupBox   Values  
    Label Label   Number 1:  
    TextBox TextBox txtNumber1 0.00 TextAlign: Right
    Label Label   Number 2:  
    TextBox TextBox txtNumber2 0.00 TextAlign: Right
    Label Label   Result:  
    TextBox TextBox txtResult 0.00 TextAlign: Right
    GroupBox GroupBox   Operation  
    RadioButton RadioButton rdoAddition Addition  
    RadioButton RadioButton rdoSubtraction Subtraction  
    RadioButton RadioButton rdoMultiplication Multiplication  
    RadioButton RadioButton rdoDivision Division  
  7. 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);
    	}
    }
  8. Display the form
  9. 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);
    	}
    }
  10. Return to the form
  11. 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);
    	}
    }
  12. Return to the form
  13. 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);
    	}
    }
  14. Test the application
 
 

Home Copyright © 2004-2006 FunctionX, Inc.