Please Help ! Radio Buttons question

  • Thread starter Dino Buljubasic
  • Start date
D

Dino Buljubasic

Hi,

I had to improvise a control that looks like a list view with each row
holding 3 radio buttons. To do this I am using a panel (mainPanel)
that holds other panels representing rows (rowPanel).

So, I fetch database and for each item, I add a rowPenel with 3 radio
buttons to the mainPanel.

Now, for each row, there must be one radio button selected. To check
that, I do:

For i = 0 To mainPanel.Controls.Count - 1
Dim rowPanel as Panel = mainPanel.Controls.item(i)
Dim aRadioButton as RadioButton

For Each aRadioButton in rowPanel.Controls
If aRadioButton.Checked = True Then
blnFound = True
End If
End For

If blnFound = False Then
ErrorMessage()
End If

End For

The problem is that even though I check a radio button, blnFound will
be still false. Can somebody tell what I am doing wrong?

Thank you
 
T

Terp

Your code looks a bit odd to me. Perhaps you are getting messed up because
there are controls on the mainPanel that aren't panels? Try something like:

Dim Ctl As Control
Dim pnlCtl As Control
For Each Ctl In mainPanel.controls
If TypeOf Ctl Is Panel Then
For Each pnlCtl In Ctl.Controls
If TypeOf pnlCtl Is RadioButton Then
blnFound = False
If pnlCtl.Checked = True Then
blnFound = True
End If
End If
Next
End If
If blnFound = False Then
ErrorMsg()
Exit For
End If
Next


Terp
 
D

Dino Buljubasic

Thank you,

It works fine now

Dino

Your code looks a bit odd to me. Perhaps you are getting messed up because
there are controls on the mainPanel that aren't panels? Try something like:

Dim Ctl As Control
Dim pnlCtl As Control
For Each Ctl In mainPanel.controls
If TypeOf Ctl Is Panel Then
For Each pnlCtl In Ctl.Controls
If TypeOf pnlCtl Is RadioButton Then
blnFound = False
If pnlCtl.Checked = True Then
blnFound = True
End If
End If
Next
End If
If blnFound = False Then
ErrorMsg()
Exit For
End If
Next


Terp
 
M

Mick Doherty

For Each aRadioButton in rowPanel.Controls
If aRadioButton.Checked = True Then
blnFound = True
End If
End For

Step through this Loop

First Iteration:
blnFound = RadioButton1.Checked

Second Iteration:
blnFound = RadioButton2.Checked

Third Iteration:
blnFound = RadioButton3.Checked

Answer this question:

What is the Value of blnFound if RadioButton3.Checked = False?


Secondly, don't use a For Each RadioButton Loop. Loop through the Controls
and check the current controls type against RadioButton...

\\\
For Each PanelControl As Control In Panel.Controls
If TypeOf PanelControl Is RadioButton Then
If blnFound = False Then
blnFound = CType(PanelControl, RadioButton).Checked
End If
End If
Next
///

Alternatively, use a Do Loop

\\\
Dim Index As Integer = 0

Do
If TypeOf Panel.Controls(Index) Is RadioButton Then
blnFound = CType(Panel.Controls(Index), RadioButton).Checked
End If
Index += 1
Loop Until blnFound = True OrElse Index >= Panel.Controls.Count
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top