Results 1 to 20 of 20

Thread: [RESOLVED] [2005] Assigning values to RadioButtons

  1. #1

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Resolved [RESOLVED] [2005] Assigning values to RadioButtons

    First off I've never used radiobuttons with winforms, only asp.net and webforms.

    I have a groupbox filled with radiobuttons. But I can't seem to find where to assign their individually checked value at in their properties.

    Am I missing something?
    Attached Images Attached Images  
    Last edited by drpcken; Apr 13th, 2006 at 04:08 PM.

  2. #2
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] Assigning values to RadioButtons

    well are you wanting more than one radio button to be able to be checked on that form? As long as you just have one radiobutton checked, then its just a matter of looping the controls in the container (the tabpage, I would image), until you find the one checkbox whose state is checked...
    VB Code:
    1. 'loops for all controls in the tabpage
    2.         For Each Ctrl As Control In Me.TabPage1.Controls
    3.             'checks to see if current control in loop is a radiobutton
    4.             If TypeOf Ctrl Is RadioButton Then
    5.                 'casts control into a radiobutton to check the "checked" property
    6.                 If DirectCast(Ctrl, RadioButton).Checked = True Then
    7.                      MessageBox.Show("here is the checked one! Name:" & Ctrl.Name)
    8.                      'code to access whatever property you have that contains the value would go here
    9.                      Exit For  'exits loop since we dont have to loop any more
    10.                 End If
    11.             End If
    12.         Next
    Last edited by gigemboy; Apr 13th, 2006 at 10:57 AM. Reason: Added "Exit For"

  3. #3

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    Just 1 checkbox will be checked. Each one has its own unique value that will be passed into a function depending on which one is checked.

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  4. #4
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] Assigning values to RadioButtons

    Then the above should work...

  5. #5

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    Thanks again! I'll give it a go!

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  6. #6
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] Assigning values to RadioButtons

    Whoops... did I miss something or do I now read "assign" the properties?? If you want to assign the value, you can use the .Tag property, and put the value in there for each of the radiobuttons... then access that property when you want to retrieve the values...

  7. #7

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    THATS what I was lookin for

    Rockin! Thanks!!!!

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  8. #8

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    Hmmm.
    I'm not sure which handler to use to check and see which RadioButton is checked.

    I'm assuming I don't have to add a click handler to each radiobutton do I?

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  9. #9
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] Assigning values to RadioButtons

    Well you can do it one of two ways...

    If you want to assign some variable the value of the checked radiobutton at the time that it was checked, then you have to have a sub that can handle the click events of all of the radiobuttons (by adding each radiobutton.click onto the handles clause of a normal radiobutton click event).

    The other way would be to just retrieve the value at whatever time you need it, by looping the controls and getting the value of the checked one, like the code already posted does...

  10. #10

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    So I can create one sub that handles ALL click events for all the radiobuttons? If so how is that accomplished?

    Or do I have to have one sub for each RadioButton?

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  11. #11

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    Ahh I figured it out:

    VB Code:
    1. Private Sub RadioStation(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles F8330.CheckedChanged, F1530.CheckedChanged, F2330.CheckedChanged
    2.         'Code
    3.     End Sub

    Thanks!!!!

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  12. #12

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    Ok this is working great except for not all my controls in here are RadioButtons, I have some labels I'm using, and its trying to check the .Checked property of the labels, which of course it can't do and throws an error. How can I tell it NOT to include anything that isn't a RadioButton? Here's my code:

    VB Code:
    1. Private Sub RadioStation(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles F8330.CheckedChanged, F8340.CheckedChanged, F8350.CheckedChanged, _
    2.                              F8365.CheckedChanged, F8380.CheckedChanged, F83100.CheckedChanged, F83125.CheckedChanged, F83150.CheckedChanged, F83165.CheckedChanged, _
    3.                              F83250.CheckedChanged, F83300.CheckedChanged, F83330.CheckedChanged, F1530.CheckedChanged, F1540.CheckedChanged, F1550.CheckedChanged, _
    4.                              F1565.CheckedChanged, F1580.CheckedChanged, F15100.CheckedChanged, F15125.CheckedChanged, F15150.CheckedChanged, F15250.CheckedChanged, _
    5.                              F15300.CheckedChanged, F2330.CheckedChanged, F2350.CheckedChanged, F2365.CheckedChanged, F2380.CheckedChanged, F23100.CheckedChanged, _
    6.                              F23125.CheckedChanged, F23150.CheckedChanged, F23165.CheckedChanged, F23250.CheckedChanged, F23300.CheckedChanged, F23330.CheckedChanged
    7.  
    8.         For Each Ctrl As RadioButton In TableLayoutPanel1.Controls
    9.  
    10.             If Ctrl.Checked Then
    11.                 MsgBox("Checked!", MsgBoxStyle.OkCancel)
    12.             End If
    13.  
    14.         Next
    15.  
    16.  
    17.     End Sub

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  13. #13
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] Assigning values to RadioButtons

    Look again at the code I posted above... you have to see if the ctrl is a radiobutton first, which is addressed by "If TypeOf Ctrl is RadioButton then".. you would also need to cast the ctrl into a radiobutton in order to access the "checked" property (once again in the code I posted)...

  14. #14

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    oops sorry I jumped ahead of myself I was testing all my radios to make sure they were being handled.

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  15. #15

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    Hmm, for the first checkbox it works great, but when I click another one it throws the error again.

    VB Code:
    1. For Each Ctrl As RadioButton In TableLayoutPanel1.Controls
    2.             If TypeOf Ctrl Is RadioButton Then
    3.                 If DirectCast(Ctrl, RadioButton).Checked Then
    4.                     MsgBox("Checked!", MsgBoxStyle.OkCancel)
    5.                     Exit For
    6.                 End If
    7.             End If
    8.         Next

    Also, exactly what does DirectCast() do?

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  16. #16
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] Assigning values to RadioButtons

    DirectCast is needed because the controls in the collection are of a generic control type, so you have to cast the control into the specific type that it is so you can access the specific properties that it has... you can use CType as well, but DirectCast has better performance, but can only be used when you know the object is the type you are casting to.

    In regards to your error.. what error are you referring to? I tested the code and it worked fine. I do see that the checkchanged event is going to be fired twice after the first click since it fires once for the checkbox that was previously checked, and then again for the checkbox that is checked, since both states changed with the click. So are you doing something else in the code that you arent showing?

  17. #17

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    Here's the exact code for the sub

    VB Code:
    1. Private Sub RadioStation(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles F8330.CheckedChanged, F8340.CheckedChanged, F8350.CheckedChanged, _
    2.                              F8365.CheckedChanged, F8380.CheckedChanged, F83100.CheckedChanged, F83125.CheckedChanged, F83150.CheckedChanged, F83165.CheckedChanged, _
    3.                              F83250.CheckedChanged, F83300.CheckedChanged, F83330.CheckedChanged, F1530.CheckedChanged, F1540.CheckedChanged, F1550.CheckedChanged, _
    4.                              F1565.CheckedChanged, F1580.CheckedChanged, F15100.CheckedChanged, F15125.CheckedChanged, F15150.CheckedChanged, F15250.CheckedChanged, _
    5.                              F15300.CheckedChanged, F2330.CheckedChanged, F2350.CheckedChanged, F2365.CheckedChanged, F2380.CheckedChanged, F23100.CheckedChanged, _
    6.                              F23125.CheckedChanged, F23150.CheckedChanged, F23165.CheckedChanged, F23250.CheckedChanged, F23300.CheckedChanged, F23330.CheckedChanged
    7.  
    8.         For Each Ctrl As RadioButton In TableLayoutPanel1.Controls
    9.             If TypeOf Ctrl Is RadioButton Then
    10.                 If DirectCast(Ctrl, RadioButton).Checked Then
    11.                     MessageBox.Show("Checked!")
    12.                     Exit For
    13.                 End If
    14.             End If
    15.         Next
    16.  
    17.  
    18.     End Sub

    It throws this error
    Unable to cast object of type 'System.Windows.Forms.Label' to type 'System.Windows.Forms.RadioButton'.

    and highlights this line of code"
    VB Code:
    1. For Each Ctrl As RadioButton In TableLayoutPanel1.Controls

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  18. #18

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons

    Here's something interesting, It doesn't throw the error until the second Radio click. After the first one, the msgbox works great, then on the second one it throws the error

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

  19. #19
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: [2005] Assigning values to RadioButtons

    Its because you specified "radiobutton" instead of "control" in the for each loop...
    VB Code:
    1. For Each Ctrl As [B]Control[/B] In TableLayoutPanel1.Controls

  20. #20

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: [2005] Assigning values to RadioButtons



    That did it! Thanks for your help and patience!!!

    In the unlikely event that I answer your question correctly, please Rate my post

    Using Visual Studio 2005 Professional

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