DotNetWebTutor.com                                                    Link to Us

radiobutton/checkboxes

A checkbox can be on or off
yes or no

The user can only select one radio button from a group

Example:

  • Start a new project
  • place 3 radio button on form
  • add a groupbox
  • place 3 radiobuttons in the groupbox

example of using radio buttons in containers

  • Run it

The form is one group and the groupbox is another
The can select one from each group

checkchanged event
fires when user clicks a control to check or uncheck it

checked property
true or false
It is true when the checkbox is checked

  • Add this code to the checkChanged event of radiobutton2:

If RadioButton2.Checked Then
MessageBox.Show("Radiobutton2 is checked")
End If

  • Run it

example of checking all radio buttons
example of checking all radiobuttons

  • start new project
  • add 3 radio buttons to a form
  • add button
  • double-click button
  • add this code

Dim message As String
If radiobutton1.checked Then
message = "first one clicked"
ElseIf radiobutton2.checked Then
message = "second one clicked"
ElseIf radiobutton3.checked Then
message = "third one clicked"
End If
MessageBox.Show(message)

  • run it

There is a better .Net version of this example
.Net supports shared event handlers
change text property of each radiobutton

  • Move this line BEFORE the button_click event handler:

Dim message As String

code for creating string in visual basic

This creates a member variable of the class that can be shared
Erase all the rest of the code in the button_click event except the code to show the messagebox
Here is what the entire code looks like at this time:
messagebox.show(message)
Its OK that message has a squiggly green line under it, we will fix that later

  • go hack to design view

design view

  • using the control key select all the radio buttons and click lighting bolt and select checkedchanged (Make sure the form is not selected too)

radio button checked changed event in visual basic
Notice it now handles all three radiobuttons
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged, RadioButton2.CheckedChanged, RadioButton1.CheckedChanged

End Sub

You get two parameters the first one is the control that fired the event
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged, RadioButton2.CheckedChanged, RadioButton1.CheckedChanged

  • Add this code:

Dim radMessage As RadioButton = CType(sender, RadioButton)
If radMessage.Checked Then
message = radMessage.Text
End If

This code casts whichever object was the sender as a radiobutton
Then it checks to see if that radio button was checked
If so it sets the message to the text of the radio button
using ctype for radiobutton
visual basic checkbox control
example of checkbox

  • start new project
  • add 3 checkboxes to a form
  • add button
  • double-click button
  • add this code

Dim message As String
message = ""
If CheckBox1.Checked Then
message += "first one clicked "
End If
If CheckBox2.Checked Then
message += "second one clicked "
End If
If CheckBox3.Checked Then
message += "third one clicked"
End If
MessageBox.Show(message)

  • run it

run it