Home

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

  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 Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    		Handles btnClose.Click
            Close()
    End Sub
    
    Private Sub rdoAddition_CheckedChanged(ByVal sender As System.Object, _
    		ByVal e As System.EventArgs) Handles rdoAddition.CheckedChanged
            Dim Number1, Number2, Result As Double
    
            If txtNumber1.Text = "" Then
                Number1 = 0
            Else
                Number1 = Convert.ToDouble(txtNumber1.Text)
            End If
    
            If txtNumber2.Text = "" Then
                Number2 = 0
            Else
                Number2 = Convert.ToDouble(txtNumber2.Text)
            End If
    
            Result = Number1 + Number2
            txtResult.Text = Result.ToString()
    End Sub
    
    Private Sub rdoSubtraction_CheckedChanged(ByVal sender As System.Object, _
    		ByVal e As System.EventArgs) Handles rdoSubtraction.CheckedChanged
            Dim Number1, Number2, Result As Double
    
            If txtNumber1.Text = "" Then
                Number1 = 0
            Else
                Number1 = Convert.ToDouble(txtNumber1.Text)
            End If
    
            If txtNumber2.Text = "" Then
                Number2 = 0
            Else
                Number2 = Convert.ToDouble(txtNumber2.Text)
            End If
    
            Result = Number1 - Number2
            txtResult.Text = Result.ToString()
    End Sub
    
    Private Sub rdoMultiplication_CheckedChanged(ByVal sender As System.Object, _
    		ByVal e As System.EventArgs) Handles rdoMultiplication.CheckedChanged
            Dim Number1, Number2, Result As Double
    
            If txtNumber1.Text = "" Then
                Number1 = 0
            Else
                Number1 = Convert.ToDouble(txtNumber1.Text)
            End If
    
            If txtNumber2.Text = "" Then
                Number2 = 0
            Else
                Number2 = Convert.ToDouble(txtNumber2.Text)
            End If
    
            Result = Number1 * Number2
            txtResult.Text = Result.ToString()
    End Sub
    
    Private Sub rdoDivision_CheckedChanged(ByVal sender As System.Object, _
    		ByVal e As System.EventArgs) Handles rdoDivision.CheckedChanged
            Dim Number1, Number2, Result As Double
    
            If txtNumber1.Text = "" Then
                Number1 = 0
            Else
                Number1 = Convert.ToDouble(txtNumber1.Text)
            End If
    
            If txtNumber2.Text = "" Then
                Number2 = 0
            Else
                Number2 = Convert.ToDouble(txtNumber2.Text)
            End If
    
            Result = Number1 / Number2
            txtResult.Text = Result.ToString()
        End Sub
  5. Test the application
 
 

Home Copyright © 2004-2010 FunctionX, Inc.