By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 1,088 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

How to detect selection of a radiobutton in an array

P: 1
I'm fairly new to vb.net. At one time - many years ago I was reasonably competent GFA basic.I have written a basic banking program in vb.net which works well and I am now trying to tidy it up and improve it. At one point in my program I have a set of 5 radiobuttons to make a selection. These were placed on the form and of course there are 5 subs to detect when a selection is made. The result is that I have variable with five possible values which are processed by a 'case select'. Although this works it is a little messy and I would like to do it in far less code. The code below shows an example but I cannot create a sub which will detect and return the array number of the selected radiobutton. Any help would be much appreciated.

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Dim Choice(4) As RadioButton
  4.     Public X As Integer
  5.  
  6.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  7.         Call TheButtons()
  8.  
  9.     End Sub
  10.  
  11.     Private Sub TheButtons()
  12.  
  13.         For I = 0 To 4
  14.             Choice(I) = New RadioButton : Choice(I).Parent = Me
  15.             Choice(I).Location = New Point(200, 100 + I * 50) : Choice(I).Text = "Button  " + I.ToString
  16.         Next
  17.  
  18.     End Sub
  19.  
  20.     Private Sub SomeThingOrOther() ' Not clear about what to put here
  21.         For I = 0 To 4
  22.             If Choice(I).Checked = True Then
  23.                 X = I ' Captures the array number of the button selected
  24.             End If
  25.         Next
  26.     End Sub
  27. End Class
Aug 28 '13 #1
Share this Question
Share on Google+
1 Reply


!NoItAll
100+
P: 296
Well - since you posted code I'm thinking you are not asking people to do your homework for you so...

The trick is to add the index of each button into the Tag property of each button, and then add your own eventhandler for all of the buttons in the array. There will be one event handler for all of your buttons. Do that like this:
Expand|Select|Wrap|Line Numbers
  1. For I = 0 to 4
  2.    'put the index into the tag property
  3.    Choice(I).Tag = I.ToString  
  4.    AddHandler Choice(I).Click, AddressOf MyChoice_Click
  5. Next I
  6.  
Now you create a sub called MyChoice_Click
Expand|Select|Wrap|Line Numbers
  1. Private Sub MyChoice_Click(Sender as Object, e as Eventargs)
  2.     Select Case CInt(Sender.Tag)
  3.        Case 0
  4.           'Do This
  5.        Case 1
  6.           'Do This
  7.        Case 2
  8.           'Do This
  9.        Case 3
  10.           'Do This
  11.        Case 4
  12.           'Do This
  13.        Case Else
  14.           'handle the error
  15.    End Select
  16.  
  17. End Sub
  18.  
Because the Tag property of each button in the array carries the objects index in the array (because you assigned it with Choice(I).Tag = I.ToString), that tag is now easily readable in the event you set up with the event handler. This allows you to know which radio button was selected and act accordingly.
What you needed to know is that each button you instantiated already had its own click event - so you could assign that to your event handler. Next you needed to know that the signature of the event you created (MyChoice_Click) used the common (Sender as object, e as EventArgs) - and the Sender as Object will contain the Tag property of the object that called the event handler. The Intellisense in VS does not show you the tag property, you have to know its there - and it is because the base Object class in .net contains the Tag property and the Radiobutton is derived from that class.
Hope this helps.
By the way - here's my code for the whole thing:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private MyButton(4) As RadioButton
  3.  
  4.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  5.         Dim iTop As Integer = 0
  6.         For I = 0 To 4
  7.             iTop += 30
  8.             MyButton(I) = New RadioButton With {.Top = iTop, .Left = 25, .Parent = Me, .Tag = I.ToString}
  9.             Me.Controls.Add(MyButton(I))
  10.             MyButton(I).Show()
  11.             AddHandler MyButton(I).Click, AddressOf MyButton_Click
  12.         Next
  13.     End Sub
  14.  
  15.     Private Sub MyButton_Click(Sender As Object, e As EventArgs)
  16.         Select Case CInt(Sender.tag)
  17.             Case 0
  18.                 'Do this
  19.             Case 1
  20.                 'Do this
  21.             Case 2
  22.                 'Do this
  23.             Case 3
  24.                 'Do this
  25.             Case 4
  26.                 'Do this
  27.             Case Else
  28.                 'handle the error
  29.         End Select
  30.     End Sub
  31. End Class
  32.  
Sep 1 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.