Jack Morton
Jack Morton

Reputation: 199

How to get all Labels to edit in Winforms?

I have 5 label that I created by drag & drop on the form. Is there an array of these labels (that's already created) that I can use?

If I wanted to enter text into all these label for example, would I able to do it through creating a loop and assign each element of labels array a particular text?

Upvotes: 0

Views: 6045

Answers (2)

Hamed
Hamed

Reputation: 2168

There is no array of labels But just a collection of controls.
you can choose the Labels from the form's control collection and enter text into all these labels.
like this method :

foreach (Control c in this.Controls)
{
    if (c.GetType() == typeof(Label))
        c.Text = "Your String";
}

Upvotes: 2

Enigmativity
Enigmativity

Reputation: 117175

The short answer is "no". There is no array already created for just the 5 labels you created using drag and drop.

You can iterate through all the children of a form (and recursively for controls placed on container controls on your form), but you would get back all the controls on the form and not just the 5 labels.

You could use a specific naming for these labels - like label_1, label_2, label_3, label_4, label_5 and filter based on .Name.Starts("label_") to allow you to update these labels specifically.

Does this help?

Upvotes: 1

Related Questions