473,395 Members | 1,568 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,395 software developers and data experts.

Dynamically added radio buttons

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
Nov 21 '05 #1
4 1177
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
Nov 21 '05 #2
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #3
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
Nov 21 '05 #4
thanks
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: obsidian8 | last post by:
Hi All, I have looked around for an answer to this question, but haven't found one as of yet. I'm trying to use javascript to dynamically create raido buttons. I am able to create them easily...
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
2
by: James P. | last post by:
Help, I need to display radio buttons on a form. The data is from SQL table: each row in each table is displayed as a radio button. I have multiple SQL tables so I understand I need to put...
7
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio...
5
by: satyabhaskar | last post by:
hi all, In my web page i have created radio buttons dynamically on to the page .....following is my code string Course, Semester, Section; int rowsCount; string con =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.