Question Radio Buton?

CoachBarker

Well-known member
Joined
Jul 26, 2007
Messages
133
Programming Experience
1-3
I have 4 radio buttons on a form, before I can run some code I want to check to see that 1 of the radio buttons is checked. IS there a simple way to do this with out a lot of Ifs?
 
got it

VB.NET:
Expand Collapse Copy
If Me.RadioButton1.Checked = False AndAlso Me.RadioButton2.Checked = False AndAlso Me.RadioButton3.Checked = False AndAlso Me.RadioButton4.Checked = False Then
     MessageBox.Show("You need to select an answer.")
     Exit Sub
End If

first few times I tired AndAlso the compiler wouldn't take it.
 
Another method would be:
VB.NET:
Expand Collapse Copy
Select Case True
  Case Me.RadioButton1.Checked
  Case Me.RadioButton2.Checked
  Case Me.RadioButton3.Checked
  Case Me.RadioButton4.Checked
  Case Else
    'None were selected
End Select

If it hits the else then you can run the code needed for none selected.

Another way of handling this would be to check one of them on form load or whatever which would mean that one of them would always be selected.
 
You know, i never thought of that (Select Case <constant> Case <variable> Case <variable>)
I always and only ever coded the other way round. THat's cool :)


Now, for another suggestion (psuedocode):

VB.NET:
Expand Collapse Copy
Dim oneRbIsChecked as Boolean = False
Dim c as Control = Me.GetNextControl()
While c IsNot Nothing AndAlso Not oneRbIsChecked 
  oneRbIsChecked = (TypeOf(c) Is RadioButton AndAlso DirectCast(c, RadioButton).Checked)
  c = c.GetNextCOntrol()
End While

This should crawl the form for all radiobuttons, stopping as soon as it finds a checked one. If none are checked, "oneRBIsChecked" will still be false when the loop exits
 
That fromat throws this error.

Error.JPG
 
You know, i never thought of that (Select Case <constant> Case <variable> Case <variable>)
I always and only ever coded the other way round. THat's cool :)

Yea, it's one of those things you can't do in C# that was irritating me a couple of weeks ago
 
That fromat throws this error.

View attachment 1697

Aside from the fact that I said "pseudocode" which means "here is the logic, not the exact syntactically correct, fully compiling exact solutoon to your problem so you dont have to put any thought into it" .. if you cannot read and understand what seems to be a reasonably clear and logical error message from the compiler, and work out how to solve it, then.. um.. maybe you should leave the programming to someone else.

Cmon man, engage brain! One day there might not be someone around to think it out for you; it seems to be a boolean parameter missing that states whether you want to go forwards or backwards (and I inferred that from the error message, not from looking up the API) so try sticking a "True" in there.. Note that you'll also need to specify a control to start from. THey will be enumerated in tab order.
Or read the docs for GetNextControl().. Heck the code is only 5 lines so it's not like it'll take a long time to reason out how it works.

Reading the documentation would have taken less time than posting here, so please, help us help you.. Help yourself along a little too

Note, this post isnt meant to belittle in any way.. Its just a kick up the backside to not rely on someone else for every little bump you hit. Hope you take it that way :)
 
Didn't take it as belittling, and everyone needs a kick now and then. Was just curious how it would work, the code I posted I used and since it works just fine I have no intention in changing it. My old eyes passed right over the words "pseudocode". :o

If I was going to use it I would have read the documentation for "GetNextControl()". ;)

No harm = No foul :D
 
Back
Top