Results 1 to 14 of 14

Thread: [RESOLVED] Checking if radio buttons checked?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Resolved [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?

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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).
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Checking if radio buttons checked?

    Quote Originally Posted by techgnome View Post
    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Posts
    136

    Re: Checking if radio buttons checked?

    Thanks guys. Both keystone_paul and techgnome's examples worked. Thanks for the help!

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Lively Member
    Join Date
    Jan 2009
    Posts
    77

    Re: [RESOLVED] Checking if radio buttons checked?

    This is the easyiest way...


    vb.net Code:
    1. If RadioButton1.checked = true then
    2. 'Code if the first radio button is checked
    3. elseif radiobutton2.checked = true then
    4. 'Code if the seconed radio button is checked
    5. end if
    Need any VB help/

    Just PM me!

  10. #10
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: [RESOLVED] Checking if radio buttons checked?

    Quote Originally Posted by tom1859 View Post
    This is the easyiest way...


    vb.net Code:
    1. If RadioButton1.checked = true then
    2. 'Code if the first radio button is checked
    3. elseif radiobutton2.checked = true then
    4. 'Code if the seconed radio button is checked
    5. end if
    umm... it may the easiest way to do something, but it doesn't do what the OP was asking for!

  11. #11
    Lively Member
    Join Date
    Jan 2009
    Posts
    77

    Re: [RESOLVED] Checking if radio buttons checked?

    Quote Originally Posted by keystone_paul View Post
    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
    Need any VB help/

    Just PM me!

  12. #12
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: [RESOLVED] Checking if radio buttons checked?

    Quote Originally Posted by tom1859 View Post
    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!

  13. #13
    Lively Member
    Join Date
    Jan 2009
    Posts
    77

    Re: [RESOLVED] Checking if radio buttons checked?

    Quote Originally Posted by keystone_paul View Post
    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:
    1. IF Radiobutton1.checked = false and RadioButton2.checked = False Then MSGBOX("Please select a gender")
    Need any VB help/

    Just PM me!

  14. #14
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width