Find All Labels On Form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to write some code which will find all of the "Label" controls on
the form and change some property.

I started off by iterating over the Page.Controls and looking the the type
of each control to see if it is a label. This does not do the trick. I am
guessing that if a control contains other controls, I am not seeing them. Is
there a property which tells me if the control has other controls within in
it? Would I then itterate thought the collection of controls of that control
looking for labels and controls which contain other controls?

Thanks in advance for your assistance.
 
You would have to write a little recursive routine that would go through
each controls collection of every control and look for Labels, and then do
something.
 
I am trying to write some code which will find all of the "Label" controls on
the form and change some property.

I started off by iterating over the Page.Controls and looking the the type
of each control to see if it is a label. This does not do the trick. I am
guessing that if a control contains other controls, I am not seeing them. Is
there a property which tells me if the control has other controls within in
it? Would I then itterate thought the collection of controls of that control
looking for labels and controls which contain other controls?

Thanks in advance for your assistance.

The control has a HasControls method. And yes, the Controls collection
contains only the direct children so you would have to iterate
recursively to find Labels (or whatever) at deeper levels.

Hans Kesting
 
Sample Code:

private void ChangeControlProperties(Control oParent)
{
int iItem;
Control oChild;
Label oChildLabel;

if ((oParent == null) || (!oParent.HasControls()))
{
return;
}

for (iItem = 0; iItem < oParent.Controls.Count; iItem++)
{
oChild = oParent.Controls[iItem];
if (!oChild.Visible)
{
continue;
}

if (oChildLabel is Label)
{
oChildLabel = (Label) oChild;
// Change Label Properties Here
}

ChangeControlProperties(oChild);
}
}

Call it as following:
ChangeControlProperties(Page);

Hth,
Ramu
 

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