Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading Radio Buttons into an Array then Looping through them

Status
Not open for further replies.

RITE1

IS-IT--Management
Joined
Oct 25, 2003
Messages
91
Location
US
Hello,

I have been trying to figure out how I can load my radio buttons into an array then loop through them to see which is checked but am having problems.

I made a radio button variable equal to a value in the radio button array (a different radio button depening on the index value via the loop) but it keeps giving me the "Object reference not set to an instance of an object" error.

Can anyone help?

Here is a snipet of the code..

Dim RadioButtonArray() As RadioButton = {rad1, rad2, rad3, rad4}

Do Until i = intcount1
RadioButton = RadioButtonArray(i)

If RadioButton.Checked = True Then
do some suff...
End If
i = +1
Loop

Thanks!!
 
You can make use of Collection Object which provides a way of grouping and managing related objects. something liek this:

Dim radioCollection As New Collection

'Add RadioButton controls to collection.
radioCollection.Add(RadioButton1)
radioCollection.Add(RadioButton2)
radioCollection.Add(RadioButton3)
radioCollection.Add(RadioButton4)
radioCollection.Add(RadioButton5)
radioCollection.Add(RadioButton6)

This is how you can find, which one is checked

Dim rd As RadioButton

'For Each...Next Statement to iterate through collection.
For Each rd In radioCollection
If rd.Checked = True Then
MessageBox.Show(rd.Text & " " & "is clicked")
End If
Next

Check out a working sample, with radio button and checkbox collection here:



 
Did you ever set the radio buttons to "New"?
 
Thanks for the help PankajBanga. Ill give it a shot. Looks like what Im looking for!

RiverGuy - I tried something like that by defining the array as a new radio button but it gave me an error. The buttons were originally created via drop and drap with vs.net. Also, the radio button variable that I created I had to use new or it would give me errors. Any more specific suggestions?

Thanks again RiverGuy and PankajBanga
 
The collection worked but...

The purpose of putting it an array was so that I would know which one was checked based on its index, and then use its coorisponding value (instead of (X,0) I would use (X,1), its a multidimensional array) to insight the correct responce.

Any suggestions on how I could make this work in an array?

Thanks again.
 
Further more, if I say;

radiobutton = rad1
if radiobutton.checked = true then
'blah
endif

It works perfectly!? Why, when I put the radio button into an array and then take it back out, is the obejct reference not set to an instance of the object?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top