Dynamically added radio buttons

  • Thread starter Thread starter Terry Holland
  • Start date Start date
T

Terry Holland

I am a vb6 programmer just moving over to vb.net.

I have an form onto which I have dynamically added a number of panels.
I have dynamically added a label and three RadioButtons to each of these
panels.

What I want to do is then scroll through each of the RadioButtons in each of
the Panels to determine whether it is Checked or not.

Im using the following code to scroll through the RadioButtons but am
getting the error
"An unhandled exception of type 'System.InvalidCastException' occurred in
StaffActions.exe

Additional information: Specified cast is not valid."

My code is
==================================================
For Each ctlPanel In Panel1.Controls

If ctlPanel.Name <> "pnlRate" Then

For Each ctlRate In ctlPanel.Controls 'ERROR ON THIS LINE

Debug.WriteLine(ctlPanel.Name & " - " &
ctlRate.GetType.ToString)

Next

End If

Next

==================================================

I'm a bit confused as I seem to be able to scroll through a collection of
Panels in my main panel (Panel1), but I can't then scroll through the
collection of RadioButtons in each of these panels.

Could someone explain the prob to me please

tia



Terry Holland
 
Terry,

This should almost do what you ask, roughly typed in this message so watch
typos

\\\
For each ctlPanel as Control In me.Controls
If TypeOf ctlPanel Is Panel then
For Each ctlRB as Control In ctlPanel.Controls
If TypeOf ctlRB Is RadioButton Then
Debug.WriteLine(ctlRB.Name & "and what you want more")
End If
Next
Next
///
I hope this helps?

Cor
 
I was guessing that this would be the way, but I was wondering why it is ok
to use

For each ctlPanel In me.Controls '''where ctlPanel is type Panel

but it is not ok to use

For Each ctlRB In ctlPanel.Controls '''where ctlRB is type RadioButton

Using your method, how would I check the Checked property of the ctlRB
control? I guess I have to cast it to RadioButton but I've never done that
 
Terry,
I was guessing that this would be the way, but I was wondering why it is ok
to use
For each ctlPanel In me.Controls '''where ctlPanel is type Panel
but it is not ok to use
For Each ctlRB In ctlPanel.Controls '''where ctlRB is type RadioButton

I do not understand you completly however I think that the answer is because
there can be other controls on the form and those do not have to be
evaluated for child controls, (while it will be no problem).
Using your method, how would I check the Checked property of the ctlRB
control? I guess I have to cast it to RadioButton but I've never done
that
A RadioButton derives from Control. So keep in mind that you do not have to
cast forever, however when it has a property that does not exist in Control,
than you can do
\\\
DirectCast(ctlRB,Radiobutton).checked
///
I hope this helps?

Cor
 
thanks


Cor Ligthert said:
Terry,
RadioButton

I do not understand you completly however I think that the answer is because
there can be other controls on the form and those do not have to be
evaluated for child controls, (while it will be no problem).

A RadioButton derives from Control. So keep in mind that you do not have to
cast forever, however when it has a property that does not exist in Control,
than you can do
\\\
DirectCast(ctlRB,Radiobutton).checked
///
I hope this helps?

Cor
 

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

Back
Top