Home c# Label C # array

Label C # array




I have on form 3 label. I want all these labels to put in an array so that later in the cycle you could do something like this:
(Windows Forms application)

for (int i = 0; i & lt; 3; i ++)
  labels [i] .text = "123";

Answer 1, Authority 100%

var labels = new [] {label1, label2, label3};

Answer 2, Authority 50%

list & lt; Control & GT; List_Labels = New List & lt; Control & GT; ();
list_labels.add (label1);
list_labels.add (label2);
list_labels.add (label3);
For (int i = 0; i & lt; list_labels.count; i ++)
  List_Labels [i] .text = "123";

Answer 3, Authority 50%

label [] labels = new label [3];
for (int i = 0; i & lt; 3; i ++)
  labels [i] = new label ();
  labels [i] .text = i.Tostring ();
  this.controls.add (labels [i]);

Answer 4

And why do you need an array?

You can do the way as in this answer:

https://stackoverflow.com/questions/4630391/get-All -Controls-of-A-Specific-Type

Create a method that will all pull.

Well, if you still need an array, you can almost also put them in the collection.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions