Results 1 to 9 of 9

Thread: Handling radio buttons

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Handling radio buttons

    I put three radio buttons on a form. Then, I wanted to handle them all from a single sub, so I made a Sub like this:

    VB Code:
    1. Private Sub RadioArray_Checked(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click
    2.  
    3.  
    4. End Sub


    Now, how can I determine which radio button was clicked, and determine it's value as well?

    TIA.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you'd be best adding a handler for all the buttons, rather that having a long sub ( saying Handles radiobutton1 , radiobutton2 .....) , here's a quick example...
    VB Code:
    1. [Color=Blue]Private[/COLOR] [Color=Blue]WithEvents[/COLOR] RadioArray [Color=Blue]As[/COLOR] RadioButton
    2.  
    3.     [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Form1_Load([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] [Color=Blue]MyBase[/COLOR].Load
    4.         [Color=Blue]Dim[/COLOR] rdBtn [Color=Blue]As[/COLOR] Control
    5.         [Color=Blue]For[/COLOR] [Color=Blue]Each[/COLOR] rdBtn [Color=Blue]In[/COLOR] [Color=Blue]MyBase[/COLOR].Controls
    6.             [Color=Blue]If[/COLOR] [Color=Blue]TypeOf[/COLOR] rdBtn [Color=Blue]Is[/COLOR] RadioButton [Color=Blue]Then
    7. [/COLOR]                [Color=Blue]AddHandler[/COLOR] rdBtn.Click, [Color=Blue]AddressOf[/COLOR] RadioArray_Click
    8.                 [Color=Green]'///[/COLOR] [Color=Green]all[/COLOR] [Color=Green]radiobutton[/COLOR] [Color=Green]clicks[/COLOR] [Color=Green]now[/COLOR] [Color=Green]handled[/COLOR] [Color=Green]by[/COLOR] [Color=Green]one[/COLOR] [Color=Green]sub[/COLOR] [Color=Green].[/COLOR] [Color=Green]
    9. [/COLOR]            [Color=Blue]End[/COLOR] [Color=Blue]If
    10. [/COLOR]        [Color=Blue]Next
    11. [/COLOR]    [Color=Blue]End[/COLOR] [Color=Blue]Sub
    12.  
    13. [/COLOR]    [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] RadioArray_Click([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] [Color=Blue]Object[/COLOR], [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] RadioArray.Click
    14.         [Color=Blue]Dim[/COLOR] btn [Color=Blue]As[/COLOR] RadioButton = [Color=Blue]DirectCast[/COLOR](sender, RadioButton)
    15.         [Color=Blue]Select[/COLOR] [Color=Blue]Case[/COLOR] btn.Name
    16.             [Color=Blue]Case[/COLOR] "RadioButton1"
    17.                 [Color=Green]'///[/COLOR] [Color=Green]carry[/COLOR] [Color=Green]out[/COLOR] [Color=Green]code[/COLOR] [Color=Green]for[/COLOR] [Color=Green]radiobutton1[/COLOR] [Color=Green],[/COLOR] [Color=Green][/COLOR] [Color=Green]eg:
    18. [/COLOR]                [Color=Blue]If[/COLOR] btn.Checked [Color=Blue]Then
    19. [/COLOR]                    [Color=Green]'///[/COLOR] [Color=Green]do[/COLOR] [Color=Green]stuff[/COLOR] [Color=Green]because[/COLOR] [Color=Green]it's[/COLOR] [Color=Green]checked[/COLOR] [Color=Green].[/COLOR] [Color=Green]
    20. [/COLOR]                [Color=Blue]Else
    21. [/COLOR]                    [Color=Green]'///[/COLOR] [Color=Green]it's[/COLOR] [Color=Green]not[/COLOR] [Color=Green]checked[/COLOR] [Color=Green].[/COLOR] [Color=Green]
    22. [/COLOR]                [Color=Blue]End[/COLOR] [Color=Blue]If
    23. [/COLOR]            [Color=Blue]Case[/COLOR] "RadioButton2"
    24.                 [Color=Green]'///[/COLOR] [Color=Green]and[/COLOR] [Color=Green]so[/COLOR] [Color=Green]on[/COLOR] [Color=Green].[/COLOR] [Color=Green][/COLOR] [Color=Green].[/COLOR] [Color=Green][/COLOR] [Color=Green].[/COLOR] [Color=Green]
    25. [/COLOR]        [Color=Blue]End[/COLOR] [Color=Blue]Select
    26. [/COLOR]    [Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    That's a great snippet you just gave.

    After some hair pulling, I came up with this:

    VB Code:
    1. Private Sub RadioArray_Checked(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click
    2.  
    3.         Dim a As RadioButton
    4.         a = CType(sender, RadioButton)
    5.         MessageBox.Show("you clicked " & a.Name & "")
    6.  
    7.     End Sub

    But obviously yours is better.

    Uhm... I'm still unaware of so many of these little methods that people use to accomplish stuff, while I was great at VB6. Can you suggest some good book for me? More practical examples rather than theoretical stuff. I already have Dan Appleman's "Moving to VB.NET", but it's very theoretical.

    Thank you.

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    VB Code:
    1. AddHandler rdBtn.Click, AddressOf RadioArray_Click

    What is this? It's not a function, because parantheses cause errors...

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    no problem little things you will pick up on ( as i did when i changed from vb6 ) are like this...
    in vb6 you had to Dim first , eg:
    VB Code:
    1. Dim a As RadioButton
    2. a = CType(sender, RadioButton)
    in vb.net you can Dim and also Set the item in one line , eg:
    VB Code:
    1. Dim a As RadioButton = CType(sender, RadioButton)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    You did a DirectCast, and I did a CType. Any difference or issues?

  7. #7
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by mendhak
    You did a DirectCast, and I did a CType. Any difference or issues?
    Ctype is for smokers
    DirectCast is faster...
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  8. #8

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by MrPolite
    Ctype is for smokers
    DirectCast is faster...

    KHAFOSHE!!

    Yeah, well, enjoy all the fumes approaching you. You'll be screaming in them soon.

  9. #9
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    ok DirectCast needs a reference type. Ctype works with value types too I guess.

    edit: ok this gives you an error

    Dim a as integer = DirectCast("1", Integer)



    there is more to it though, I cant remember
    Last edited by MrPolite; Oct 31st, 2003 at 01:27 AM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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