471,337 Members | 862 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

loop over the controls in my form

hi!
i have a class chooseLevel : Form
and i need to loop over all the radio buttons that are on my form, and
check their text. if their text is smaller than 4, i got to disable
them.
how do i do it?
thanx alot

Feb 22 '06 #1
6 1538
foreach (Control c in this.Controls)
{
RadioButton button = c as RadioButton;
if (button != null && button.Text.Length < 4)
{
button.Enabled = false;
}
}

Feb 22 '06 #2
it doesn't help.
i get in this.controls.count- 0
which means that although i put it on the form, it doesn't count it

Feb 22 '06 #3
oops sorry, i forgot to initialize the componnets. now it's fine
thanx

Feb 22 '06 #4
The sample posted will work, but only for controls that arent nested in other
controls (such as a groupbox, which is very common).

Here's an exercise I did some time ago that illustrates one technique for
"walking the control hierarchy" and identifying a control at any level:

http://www.eggheadcafe.com/articles/20040506.asp

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"be********@gmail.com" wrote:
hi!
i have a class chooseLevel : Form
and i need to loop over all the radio buttons that are on my form, and
check their text. if their text is smaller than 4, i got to disable
them.
how do i do it?
thanx alot

Feb 23 '06 #5
Thank you Peter.

Peter is right, particularly where radio buttons are concerned: they're
most often grouped within group boxes, and in that case my code snippet
won't find them.

Feb 23 '06 #6
Note that if panels etc get involved then you will need to recursively check
the .Controls of each Control - otherwise you will only look *directly* on
the form; e.g. (setting a tooltip in this case):

private void CascadeTooltip(Control control, string caption) {
toolTip.SetToolTip(control, caption);
foreach (Control subControl in control.Controls)
CascadeTooltip(subControl, caption);
}

Note that the form itself is a Control, so it can call CascadeTooltip(this,
"some value");

Marc
Feb 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ryan Liu | last post: by
4 posts views Thread by Ryan Ternier | last post: by
8 posts views Thread by dominique | last post: by
1 post views Thread by Franck | last post: by
reply views Thread by rosydwin | last post: by

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.