|
-
Oct 30th, 2003, 11:53 PM
#1
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:
Private Sub RadioArray_Checked(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click
End Sub
Now, how can I determine which radio button was clicked, and determine it's value as well?
TIA.
-
Oct 31st, 2003, 12:28 AM
#2
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:
[Color=Blue]Private[/COLOR] [Color=Blue]WithEvents[/COLOR] RadioArray [Color=Blue]As[/COLOR] RadioButton
[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
[Color=Blue]Dim[/COLOR] rdBtn [Color=Blue]As[/COLOR] Control
[Color=Blue]For[/COLOR] [Color=Blue]Each[/COLOR] rdBtn [Color=Blue]In[/COLOR] [Color=Blue]MyBase[/COLOR].Controls
[Color=Blue]If[/COLOR] [Color=Blue]TypeOf[/COLOR] rdBtn [Color=Blue]Is[/COLOR] RadioButton [Color=Blue]Then
[/COLOR] [Color=Blue]AddHandler[/COLOR] rdBtn.Click, [Color=Blue]AddressOf[/COLOR] RadioArray_Click
[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]
[/COLOR] [Color=Blue]End[/COLOR] [Color=Blue]If
[/COLOR] [Color=Blue]Next
[/COLOR] [Color=Blue]End[/COLOR] [Color=Blue]Sub
[/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
[Color=Blue]Dim[/COLOR] btn [Color=Blue]As[/COLOR] RadioButton = [Color=Blue]DirectCast[/COLOR](sender, RadioButton)
[Color=Blue]Select[/COLOR] [Color=Blue]Case[/COLOR] btn.Name
[Color=Blue]Case[/COLOR] "RadioButton1"
[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:
[/COLOR] [Color=Blue]If[/COLOR] btn.Checked [Color=Blue]Then
[/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]
[/COLOR] [Color=Blue]Else
[/COLOR] [Color=Green]'///[/COLOR] [Color=Green]it's[/COLOR] [Color=Green]not[/COLOR] [Color=Green]checked[/COLOR] [Color=Green].[/COLOR] [Color=Green]
[/COLOR] [Color=Blue]End[/COLOR] [Color=Blue]If
[/COLOR] [Color=Blue]Case[/COLOR] "RadioButton2"
[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]
[/COLOR] [Color=Blue]End[/COLOR] [Color=Blue]Select
[/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]
-
Oct 31st, 2003, 12:41 AM
#3
That's a great snippet you just gave.
After some hair pulling, I came up with this:
VB Code:
Private Sub RadioArray_Checked(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click
Dim a As RadioButton
a = CType(sender, RadioButton)
MessageBox.Show("you clicked " & a.Name & "")
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.
-
Oct 31st, 2003, 12:48 AM
#4
VB Code:
AddHandler rdBtn.Click, AddressOf RadioArray_Click
What is this? It's not a function, because parantheses cause errors...
-
Oct 31st, 2003, 12:51 AM
#5
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:
Dim a As RadioButton
a = CType(sender, RadioButton)
in vb.net you can Dim and also Set the item in one line , eg:
VB Code:
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]
-
Oct 31st, 2003, 01:02 AM
#6
You did a DirectCast, and I did a CType. Any difference or issues?
-
Oct 31st, 2003, 01:19 AM
#7
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!!
-
Oct 31st, 2003, 01:22 AM
#8
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.
-
Oct 31st, 2003, 01:22 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|