Question

I have successfully created a list of 10 radio buttons in a group. Each radio button has its own name,

RadioB0, RadioB1, RadioB2, etc.

I would like to change this to an array, RadioB[10]. Having the index of the radio button on an event simplifies the code I have to write immensely.

I don't see a Toolbox item to do this. I'm using Visual Studio 2010.

I know I could create a RadioButtonList in code, but I would then have to determine where to put each of the individual radio buttons in code as well.

How can I layout my radio buttons in a form, but still have them be part of a RadioButtonList?

Years ago I vaguely remember this being easy in VB6.

Was it helpful?

Solution

Most of the original really good reasons to use Control Arrays went away a long time ago. They are still there, you just have to add them. Left Click on the Toolbar/box and select "Choose Items", scroll down and select RadioButtonArray - and just like that you are now 15 years in the past.

Another thing you can do is loop thru the parent control's control array. If you add these to something like a Panel, you can do a For Each loop and do whatever you need there.

For Each rb As RadioButton In pnlRadios.Controls
      ' do your thing
Next

If there are other controls on the panel, you will have to use GetType to test that the control is one of your RadioButtons before fiddling with it.

Building your own RadioButtonList is probably easier than you think since it can be done in a loop. The big thing likely to change is .Top which is just a multiple of Height plus the .Top of RB(0).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top