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

Recommended Answers

All 2 Replies

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);
                }
            }

thanks ddanbe. :icon_smile:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.