Using Radio Buttons


 

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:

Radio Buttons

Text Boxes

Panel

Labels

Buttons

  1. Start Microsoft Visual C# and create a Windows Application named Calculation
  2. Design the form as follows:
     
     
    Control Name Text Other Properties
    Panel      
    Label   Number &1:  
    TextBox txtNumber1   Text: 0.00
    TextAlign: Right
    Label   Number &2:  
    TextBox txtNumber2   Text: 0.00
    TextAlign: Right
    Label   Result:  
    TextBox txtResult 0.00 Text: 0.00
    TextAlign: Right
    Panel      
    RadioButton rdoAddition &Addition CheckAlign: MiddleRight
    RadioButton rdoSubtraction &Subtraction CheckAlign: MiddleRight
    RadioButton rdoMultiplication &Multiplication CheckAlign: MiddleRight
    RadioButton rdoDivision &Division CheckAlign: MiddleRight
    Button btnClose Close  

     

  3. Double-click the Close button and double-click each radio button
  4. Implement their Click events as follows:
     
    private void btnClose_Click(object sender, System.EventArgs e)
    		{
    			Close();
    		}
    
    		private void rdoAddition_CheckedChanged(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);
    			}
    		}
    
    		private void rdoSubtraction_CheckedChanged(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);
    			}
    		}
    
    		private void rdoMultiplication_CheckedChanged(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);
    			}
    		}
    
    		private void rdoDivision_CheckedChanged(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);
    			}
    		}
    	}
  5. Test the application.

 


Home Copyright © 2004-2010 FunctionX, Inc.