- 1). Create a new Visual Basic project. Open Visual Studio, click "File," then "New Project." When the new project window appears, choose Visual Basic, then Windows from the tree view. Click the Windows Form Project icon, enter the project name and click "OK." After a brief wait, a new form will appear.
- 2). Create a radio button list on the form. Click the toolbox icon on the tool bar then drag a Group Box control onto the form. Drag four radio buttons from the toolbox onto the group box. Arrange them vertically, one on top of the other.
- 3). Add captions "1," "2," "3" and "4" to the radio buttons. Right-click the first radio button, then click "Properties." Change the text property to "1," then click the "Escape" key to close the property box. Repeat this step for each radio button, changing each text property to "2," "3" and "4."
- 4). Drag three buttons from the toolbox and place them outside the group box. Use the same process to set the text properties to "Even," "Odd" and "All." Change their Name properties to "btnEven," "btnOdd" and "btnAll."
- 5). Add code to enable only the even radio buttons when the Even button gets clicked. Double-click the "Even" button and a new code window will appear. Paste the following code into the new event handler:
Private Sub btnEven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEven.Click
RadioButton1.Enabled = False
RadioButton2.Enabled = True
RadioButton3.Enabled = False
RadioButton4.Enabled = True
RadioButton2.Checked = True
End Sub
This code sets the enabled property of buttons "2" and "4" to "True," allowing them to be active, while setting buttons "1" and "3" to "False," preventing them from being checked. The last line sets the checked property of radio button "2" to "True." - 6). Right-click the code window then click "View Designer" to return to the form design window. Double-click the "Odd" button, then add the following code:
Private Sub btnOdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOdd.Click
RadioButton1.Enabled = True
RadioButton2.Enabled = False
RadioButton3.Enabled = True
RadioButton4.Enabled = False
RadioButton1.Checked = True
End Sub - 7). Run the program. Click "F5," then click each radio button. All four buttons respond as expected. Now click the "Even" button and only radio buttons "2" and "4" will respond. Click the "Odd" button and only radio buttons "1" and "3" will respond.
previous post