6 Replies - 3061 Views - Last Post: 15 November 2011 - 11:30 AM Rate Topic: -----

#1 powerzan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-November 11

How to check which RadioButton is checked

Posted 15 November 2011 - 09:38 AM

I have problem, that i have a lot of code, and I want to try to make less as much code. I have 30 RadioButtons and is even possible tu use something like that
If CheckedRadioButton.Checked = True Then
? Thanks for all your help.

Oh and, when user click on any of RadioButtons, then it can write number of RadioBUtton in settings.
Is This A Good Question/Topic? 0
  • +

Replies To: How to check which RadioButton is checked

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9287
  • View blog
  • Posts: 34,811
  • Joined: 12-June 08

Re: How to check which RadioButton is checked

Posted 15 November 2011 - 09:44 AM

Yes.. the radio button object has the "checked" property. True = selected, false = not selected.


Quote

Oh and, when user click on any of RadioButtons, then it can write number of RadioBUtton in settings.


Radio buttons also have the event "CheckedChanged"... when that fires off do your action.
Was This Post Helpful? 1
  • +
  • -

#3 powerzan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-November 11

Re: How to check which RadioButton is checked

Posted 15 November 2011 - 09:50 AM

I know that RadioButton have Checked property, but I want to check in 1 line, which one is checked.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9287
  • View blog
  • Posts: 34,811
  • Joined: 12-June 08

Re: How to check which RadioButton is checked

Posted 15 November 2011 - 09:53 AM

You can't magically check with one line which (out of thirty) are checked. That's just a silly notion. You could bastardize the switch statement to something like:

Select Case True
  Case radiobuttonA.checked
 '-- stuff
  Case radiobuttonB.checked
 '-- stuff
  Case radiobuttonC.checked
 '-- stuff
End Select



but that won't help ya if you need multiple buttons checked. Keep with the if statement!
Was This Post Helpful? 2
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How to check which RadioButton is checked

Posted 15 November 2011 - 09:54 AM

Yes you can use If else to check for the selected, and if user check one, you can use check change event to see if the radio button is checked or not
Was This Post Helpful? 1
  • +
  • -

#6 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: How to check which RadioButton is checked

Posted 15 November 2011 - 11:13 AM

It's hard to know exactly which way to go with this without knowing what you're trying to do. Whatever it is, you're going to end up with a lot of code when dealing with 30 controls. Since they're radio buttons, I'm guessing only 1 out of 30 can be checked at a time (unless you've got 5 different groups of 6 buttons each or something like that). If you're trying to figure out which one is checked, you could use the CheckChanged event handler, have it handle the event for all 30 buttons, run a single Checked check on the sender (remember, CheckChanged will fire twice whenever a new radio button is checked - once for the button that's unchecking, and once for the button that's being checked), and do whatever you need to do from there.
Private Sub rdo1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdo1.CheckedChanged, rdo2.CheckedChanged ... rdo30.CheckedChanged

    Dim rdoChecked As RadioButton = CType(sender, RadioButton)
    If rdoChecked.Checked Then

        Dim strRdoBtnName As String = rdoChecked.Name
        'If you wanted the number of the button being checked, and all your
        'buttons were named rdo1 .. rdo30 sequentially, you could get the
        'number using a substring
        Dim strBtnNumber As String = strRdoBtnName.Substring(3)
        Dim intBtnNumber As Integer = Integer.Parse(strBtnNumber)
        '...

    End If
End Sub

Was This Post Helpful? 1
  • +
  • -

#7 powerzan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-November 11

Re: How to check which RadioButton is checked

Posted 15 November 2011 - 11:30 AM

Thanks everyone for help, especially Tryparse (don't worry I have pressed green button) :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1