i have this situation, i have a winform which has arround 10 to 15 lable and comboboxes which are all not visible. now at runtime i am passing a how many to of these to be activated? their name properties are from lable1 to lable16 and combobox1 to combobox16. i am using the following code only to test if i can find the controls but it seems to fail all, ho and by the was these controls and comboboxes are over a pannel.
the following is the code i used:
foreach (Control ctrl 开发者_如何学编程in this.Controls)
{
if (ctrl is Label)
{
MessageBox.Show(((Label)ctrl).Text);
}
}
You will have to replace this.Controls
with this.Panel1.Controls
or what ever your panel control name is.
Also mayne rather have a look at Control.ControlCollection.Find Method to make use of the control name.
You have to make this recursive.
The ctrl
itself also has a Controls
. If you just walk the entire tree, you will eventually find all controls.
You have actually sort of answered your own question...
You don't find the controls in this.Control
s but Panel1.Controls
:)
you can check whether the current control (ctrl) has controls or not in order to loop through them
e.g.:
if (ctrl.Controls.Count >0)
{
//do your logic here
}
精彩评论