Help with group boxes/radio buttons

Vb Noob

Member
Joined
Oct 12, 2005
Messages
14
Programming Experience
Beginner
I have 6 group boxes and among them 22 radio buttons total.


I either want to be able to:

At least use one button to uncheck all the buttons ( i know i could do this a long way, but I want to know an easy way to do it ), or be able to only allow one of the radio buttons among the 6 group boxes to be checked at one time.

I don't want a lot of code, so if there's a simple way to do this it would be great.

I am using VB.net 2005
 
the whole point to radio buttons is to only allow one to be selected at a time, so you cant have all the radio buttons checked at the same time in the same container (groupbox in this case)

what i would do is use checkbox's instead then provide an additional checkbox that when checked all the other checkboxes become checked (and unchecked means all other checkbox's are unchecked)

in the CheckedChanged event (of the additional checkbox) you can loop through all of the controls in the groupbox and if it's a checkbox and not of the additional one, set it's checked property to true, this means that if later other checkbox's are added (or some are removed) you wouldnt need to change the code in the loop

i could write a quick example if needed to help you understand what i'm talking about
 
the whole point to radio buttons is to only allow one to be selected at a time, so you cant have all the radio buttons checked at the same time in the same container (groupbox in this case)

I don't want them all checked at the same time, but rather only ONE out of all the 22 in 6 different group boxes to be checked at one time.

I see what you're discribing but is there a way I can just uncheck all the 22 radio buttons in the 6 different group boxes with a button? I know I could do this by writing each one setting them to false, but is there an easier way?
 
Sorry to post again but my friend told me this would go through all the radio buttons I have on the form and uncheck them, but it doesn't work;

VB.NET:
Expand Collapse Copy
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ctrl [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] ctrl [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]TypeOf[/COLOR][/SIZE][SIZE=2] (ctrl) [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] RadioButton [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2](ctrl, RadioButton).Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
This is because 'Me' is the form container, but you have put your radiobuttons in different other containers. The default behaviour of radiobuttons is that they group according to what container they are placed in. That you want only one checked of radiobuttons placed in different containers sounds as a design error, but it can be done. Look up the GetNextControl method, it will let you iterate all controls no matter what container they belong, then you check if the control is a radiobutton and that it is not the radiobutton currently clicked. Here is a code example with 4 radiobuttons placed in different containers where only one of them can be checked:
VB.NET:
Expand Collapse Copy
Private Sub RadioButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click, RadioButton4.Click
  Dim ctrl As Control = Me.GetNextControl(Me, True)
  While ctrl IsNot Nothing
    If TypeOf ctrl Is RadioButton AndAlso ctrl IsNot sender Then
      DirectCast(ctrl, RadioButton).Checked = False
    End If
    ctrl = Me.GetNextControl(ctrl, True)
  End While
End Sub
 
Thanks, that solved my problem... I know I could have put them all in one group box but I didn't want to, I wanted to FIGURE it out this way. It was rather a DESIGNER CHOICE lawl...

Thanks though, I understand kinda but can you explain (i know it's basically the same) but a procedure that would clear all radio buttons checked.
 
It's that it does, it clears all (4++) radiobuttons except the one clicked.
 
Don't exclude the one clicked when unchecking, then.
 
Back
Top