[RESOLVED] Checking if radio buttons checked?
Is there a way to check whether or not the radio buttons within a group box have been checked? I have been able to check if one radio button is checked, but I would like to know if I can check for both within the same if statement, or if I would have to write a new one for the second radio button. Or can I just use the groupbox and check for selected radio buttons from that? This is what I have so far:
If Not rbMale.Checked Or Not rbFemale.Checked Then
ErrorProvider1.SetError(gbGender, "You must select your gender.")
Else
ErrorProvider1.SetError(gbGender, "")
End If
Not it still displays the error message if the female button is checked. Any help?
Re: Checking if radio buttons checked?
Normally only one radio button in a groupbox can be checked at a time. (you can't have two checked).
Re: Checking if radio buttons checked?
You just need to change your if statment to :
Code:
If Not rbMale.Checked And Not rbFemale.Checked Then
ErrorProvider1.SetError(gbGender, "You must select your gender.")
Else
ErrorProvider1.SetError(gbGender, "")
End If
Two other options spring to mind - one would be to set one as a default, ie select male by default and then there will always be one selected value, so no need to check at all.
The other option would be to have a single routine added as a handler to both buttons which sets the tag property of the groupbox to true when either button is selected, then in your if statement you'd just check the tag of the groupbox, ie
Code:
Private Sub RadioButtonCheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbMale.CheckedChanged, rbFemale.CheckedChanged
gbGender.Tag = "selected"
End Sub
Re: Checking if radio buttons checked?
You over notted your statement...
think it through... the rbFemale was selected, so it's value is true.... so the Not rbFemale.Checked will return false... BUT that means rbMale was not checked... so "Not rbMale.Checked" will return True... and displays the error.
What you want to do is check both and stop if neither one is checked.
Code:
If Not (rbMale.Value or rbFemale.Value) Then
-tg
Re: Checking if radio buttons checked?
Gary - true... but it is possible to have both unchecked.
Paul - that's overcomplicating the issue. It's a straight up logic problem that is eaasily fixed by getting the Nots correct.
-tg
Re: Checking if radio buttons checked?
Quote:
Originally Posted by
techgnome
Paul - that's overcomplicating the issue. It's a straight up logic problem that is eaasily fixed by getting the Nots correct.
-tg
True... for two options anyway, however if you have a groupbox with say 10 options, the simple logic becomes somewhat more cumbersome.
Re: Checking if radio buttons checked?
Thanks guys. Both keystone_paul and techgnome's examples worked. Thanks for the help!
Re: Checking if radio buttons checked?
At that point, I'd probably write a function that takes a container as a parameter, loop through all of the option buttons and see if anyone of them has been set.
-tg
Re: [RESOLVED] Checking if radio buttons checked?
This is the easyiest way...
vb.net Code:
If RadioButton1.checked = true then
'Code if the first radio button is checked
elseif radiobutton2.checked = true then
'Code if the seconed radio button is checked
end if
Re: [RESOLVED] Checking if radio buttons checked?
Quote:
Originally Posted by
tom1859
This is the easyiest way...
vb.net Code:
If RadioButton1.checked = true then
'Code if the first radio button is checked
elseif radiobutton2.checked = true then
'Code if the seconed radio button is checked
end if
umm... it may the easiest way to do something, but it doesn't do what the OP was asking for!
Re: [RESOLVED] Checking if radio buttons checked?
Quote:
Originally Posted by
keystone_paul
umm... it may the easiest way to do something, but it doesn't do what the OP was asking for!
Yes it does, it checks if two checkboxes are checked in one group box, also it is a single statment
Re: [RESOLVED] Checking if radio buttons checked?
Quote:
Originally Posted by
tom1859
Yes it does, it checks if two checkboxes are checked in one group box, also it is a single statment
There's some discrepancy between the description of the problem and the code.
I know that the description of the problem is misleading but looking at the code in the original post, and the comment about the suggested solutions working, what the OP is trying to do is trap the situation where there are no radio buttons selected.
Your code can easily be extended to do this but as it stands it doesn't actually achieve what he set out to do!
Re: [RESOLVED] Checking if radio buttons checked?
Quote:
Originally Posted by
keystone_paul
There's some discrepancy between the description of the problem and the code.
Looking at the code in the original post, and the comment about the suggested solutions working, what the OP is trying to do is trap the situation where there are no radio buttons selected.
Your code can easily be extended to do this but as it stands it doesn't actually achieve what he set out to do!
OK then
VB.NET Code:
IF Radiobutton1.checked = false and RadioButton2.checked = False Then MSGBOX("Please select a gender")
Re: [RESOLVED] Checking if radio buttons checked?
If using VS2008 or higher here is a generic method
Code:
If gbGender.HasRadioButtonSelectionMade Then
ErrorProvider1.SetError(gbGender, "")
Else
ErrorProvider1.SetError(gbGender, "Please make a selection")
End If
Code:
<System.Runtime.CompilerServices.Extension()> _
Public Function HasRadioButtonSelectionMade(ByVal container As Control) As Boolean
Dim Button As New RadioButton
Dim Result As Boolean = False
Dim RadioCollection = From Control In container.Controls _
Where TypeOf Control Is RadioButton _
Select Control
If Not RadioCollection Is Nothing Then
Dim CheckedButton = _
( _
From Item In RadioCollection.Cast(Of RadioButton)() _
Where Item.Checked).DefaultIfEmpty(Button).First
If Not CheckedButton.Name.Equals(Button.Name) Then
Button = CheckedButton
Result = True
End If
End If
Return Result
End Function