473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to detect selection of a radiobutton in an array

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
1 4433
!NoItAll
297 100+
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

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

Similar topics

11
by: William Gill | last post by:
I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep references to them in a 2 dimensional list ( rBtns ). It works fine, and I can even make it so only one button per column can be...
7
by: Nick Calladine | last post by:
Hi On my form i have multiple select which all have an id value total1, total2, total3 etc so i am trying to detect how many there are and then use this to caculate a total. Is there a...
4
by: Ricky W. Hunt | last post by:
Is there an easy, flexible way to determine which radiobutton is checked within a groupbox without having to code for each button explicitly? It seems there would be some kind of index you could...
5
by: John Devlon | last post by:
Hi everyone ... does anyone know if it's possible to create an array, filled with radionbuttons .... .... radiobuttons that allready exists on the form .... for example ... Dim array()...
4
by: Bmack500 | last post by:
I have the following bit of code: j2 = 0 If srvSC(j1) = "XCP CD Proxy" Then ReDim Preserve badGuys(j2) badGuys(j2) = j1 j2 += 1 End If Using .net methods, how can I detect if the badGuys...
6
by: frizzle | last post by:
Hi there. I have this array: name => john, age => 45, profession => teacher name => hank, age => 22, profession => student name => mary, age => 36, profession => dancer etc. etc. It's...
4
by: Igor | last post by:
RadioButton have property Checked (true or false). If I make RadioButtons with code, without drawing by mouse than I have problems. I write like this: Dim rb(0 To 2) As RadioButton Dim n As...
7
by: Igor | last post by:
Can I create control array (like TextBox array) in design mode. In vb6 I drow some control at the form and then I copy/paste controls. After that every control have the same name, but they have...
3
by: Hodld | last post by:
Hi, I'm creating a selection form with array data, using a for loop. This is my first time doing this, so it could be a pretty dumb mistake, but all the values end up printing on the line above...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.