Calculations With 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:
Panel
Button
Scroll Bars

  1. Start Microsoft Visual J# 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;
    
    		if( txtNumber1.get_Text() == "" )
    			Number1 = 0;
    		else
    			Number1 = System.Convert.ToDouble(txtNumber1.get_Text());
    
    		if( txtNumber2.get_Text() == "" )
    			Number2 = 0;
    		else
    			Number2 = System.Convert.ToDouble(txtNumber2.get_Text());
    
    		Result = Number1 + Number2;
    		txtResult.set_Text(String.Format("{0}", (System.Double)Result));
    	}
    
    	private void rdoSubtraction_CheckedChanged (Object sender, System.EventArgs e)
    	{
    		double Number1, Number2, Result;
    
    		if( txtNumber1.get_Text() == "" )
    			Number1 = 0;
    		else
    			Number1 = System.Convert.ToDouble(txtNumber1.get_Text());
    
    		if( txtNumber2.get_Text() == "" )
    			Number2 = 0;
    		else
    			Number2 = System.Convert.ToDouble(txtNumber2.get_Text());
    
    		Result = Number1 - Number2;
    		txtResult.set_Text(String.Format("{0}", (System.Double)Result));
    	}
    
    	private void rdoMultiplication_CheckedChanged (Object sender, System.EventArgs e)
    	{
    		double Number1, Number2, Result;
    
    		if( txtNumber1.get_Text() == "" )
    			Number1 = 0;
    		else
    			Number1 = System.Convert.ToDouble(txtNumber1.get_Text());
    
    		if( txtNumber2.get_Text() == "" )
    			Number2 = 0;
    		else
    			Number2 = System.Convert.ToDouble(txtNumber2.get_Text());
    
    		Result = Number1 * Number2;
    		txtResult.set_Text(String.Format("{0}", (System.Double)Result));
    	}
    
    	private void rdoDivision_CheckedChanged (Object sender, System.EventArgs e)
    	{
    		double Number1, Number2, Result;
    
    		if( txtNumber1.get_Text() == "" )
    			Number1 = 0;
    		else
    			Number1 = System.Convert.ToDouble(txtNumber1.get_Text());
    
    		if( txtNumber2.get_Text() == "" )
    			Number2 = 0;
    		else
    			Number2 = System.Convert.ToDouble(txtNumber2.get_Text());
    
    		Result = Number1 / Number2;
    		txtResult.set_Text(String.Format("{0}", (System.Double)Result));
    	}
  5. Test the application.

Home Copyright 2004 FunctionX, Inc.