Walter Kindblad
Walter Kindblad

Reputation: 37

Change label text with an array

So, I simply want to dynamically change my what label is currently edited through an array, to achieve as little code as possible.

This is what it looks like right now:

string[] poängLabels = new string[10];

for (int i = 3; i <= 9; i++)
{
    poängLabels[i] = ("label{0}.Text" + i);
}

Upvotes: 0

Views: 1068

Answers (2)

Sinatr
Sinatr

Reputation: 21999

You have to be able to enumerate controls somehow, putting (or having them) in the array is one option:

var result = new { label1, label2, ... }.Select(label => label.Text).ToArray();

Upvotes: 0

Taha Paksu
Taha Paksu

Reputation: 15616

You should do it like this:

poängLabels[i] = ((Label)this.Controls.Find("label" + i)).Text;

or

poängLabels[i] = ((Label)this.Controls["label" + i]).Text;

Upvotes: 1

Related Questions