Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Radio Buttons in Visual Basic
07-28-2010, 01:18 AM (This post was last modified: 07-28-2010 01:18 AM by drdebcol.)
Post: #1
Using Radio Buttons in Visual Basic
The radio button is also a very useful control in Visual Basic 2008. However, it operates differently from the check boxes. While the checkboxes work independently and allows the user to select one or more items , radio buttons are mutually exclusive, which means the user can only choose one item only out of a number of choices. Here is an example which allows the users to select one color only.

First, this picture :
[Image: vb_radiobutton.gif]
Code:
Dim strColor As String

Private Sub RadioButton8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton8.CheckedChanged
strColor = "Red"
End Sub

Private Sub RadioButton7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton7.CheckedChanged
strColor = "Green"
End Sub

Private Sub RadioYellow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioYellow.CheckedChanged
strColor = "Yellow"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label2.Text = strColor
End Sub
Although the user may only select one item at a time, he may make more than one selection if those items belong to different categories. For example, the user wish to choose T-shirt size and color, he needs to select one color and one size, which means one selection in each category. This is easily achieved in VB2008 by using the Groupbox control under the containers categories. After inserting the Groupbox into the form, you can proceed to insert the radio buttons into the Groupbox. Only the radio buttons inside the Groupbox are mutually exclusive, they are not mutually exclusive with the radio buttons outside the Groupbox. In Example 18.2, the users can select one color and one size of the T-shirt.

Second, this picture :
[Image: vb_radiobutton2.gif]

Code:
Dim strColor As String
Dim strSize As String

Private Sub RadioButton8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton8.CheckedChanged
strColor = "Red"
End Sub

Private Sub RadioButton7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton7.CheckedChanged
strColor = "Green"
End Sub

Private Sub RadioYellow_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioYellow.CheckedChanged
strColor = "Yellow"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label2.Text = strColor
Label4.Text = strSize
End Sub


Private Sub RadioXL_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioXL.CheckedChanged
strSize = "XL"
End Sub

Private Sub RadioL_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioL.CheckedChanged
strSize = "L"
End Sub

Private Sub RadioM_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioM.CheckedChanged
strSize = "M"
End Sub

Private Sub RadioS_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioS.CheckedChanged
strSize = "S"
End Sub

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
07-28-2010, 01:20 AM
Post: #2
RE: Using Radio Buttons in Visual Basic
Very simple component, yet very useful! Thanks for the info Drdebcol!

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
07-28-2010, 01:23 AM
Post: #3
RE: Using Radio Buttons in Visual Basic
(07-28-2010 01:20 AM)Back_track Wrote:  Very simple component, yet very useful! Thanks for the info Drdebcol!
Well it is easy, and very useful in GUI & Object Orientated programming !!!

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: