0

hi there,

there are 9 labels on the form with the name say Label1, Label2, Label3, ...
now i would like to set their text to "". is there anyway to use a loop and find them just by knowing their name.

any help is appreciated,
Sean

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by aulason
0

A form has a ControlsCollection which is called Controls.
You can loop through that with a foreach. Something like this:

// look through the controls of the form
            foreach (Control c in this.Controls)
            {
                if (c is Label) //see if it is a Label control
                {
                    // now do something with it
                    MessageBox.Show(c.Name);
                }
            }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.