Dlorwisdom
Dlorwisdom

Reputation: 117

Assign a variable to increment and refer to a series of label

How would one go about assigning a label control's ".Text" property to a variable. For example

when X = 1 it refers to Label1.Text.

when X = 2 it refers to Label2.Text

I ask because I want to update the .Text property of different labels, each of which are associated with an array of values, depending on the value of the corresponding array section.

Upvotes: 0

Views: 75

Answers (2)

Trevor Pilley
Trevor Pilley

Reputation: 16393

You can create an array containing a reference to the labels and access it via the index of the label you want:

var labels = new [] { Label1, Label2, Label3 }

labels[i].Text = "Foo";

Things to note - the index starts at 0 whereas your labels start at 1 so labels[0] is Label1

Upvotes: 2

p e p
p e p

Reputation: 6674

This is simple conditional logic:

string labelText;
if(x==1){
  labelText = Label1.Text;
}
else if (x==2){
  labelText = Label2.Text;
}

Upvotes: 0

Related Questions