0

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);
}
1
  • So you have 10 lables and you want to get their Texts in to an array of 10 string ? Commented Apr 4, 2017 at 11:10

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

17 Comments

It won't accept ".Find" as an argument. Any idea why?
what's your .net framework version? and are the labels inside a container element?
@WalterKindblad Are you using WPF or WinForms?
.NET Framework version is 4.0, and how do I know whether it's inside a container element? Sorry..
@ManfredRadlwimmer WinForms
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.