0

I have created a for loop, and I wanted to add random text, loaded in from an array, to a different label each time the loop runs through. I have all the labels added to my form, called lbl1, lbl2, lbl3 etc.

Random r = new Random();

        for (int i = 1; i <= 4; i++)
        {
            int randomtext = r.Next(0, mytext.GetLength(0)); //choosing random element of array to be added to label
            //this is where I want to add the text to a random label
        }

How can I add the text to a random label each time? Would I need to create an array or list of some kind? I was hoping there would be some way of just adding i as the suffix of the variable name each time. Sorry if this is obvious, I am new to C#.

Thanks!

2
  • Is this Windows forms (desktop app), WPF, MVC or Web Forms? Commented Mar 1, 2016 at 20:35
  • I'm assuming he is on the desktop. So probably WinForms or WPF? Commented Mar 1, 2016 at 20:38

3 Answers 3

2

There are several ways to do this depending on which framework you are using.

In Windows Forms you can use Controls["lbl1"] for instance, so in your case you would write the loop as follows:

Random r = new Random();
for (int i = 1; i <= 4; i++)
{
   var randomtext = r.Next(0, mytext.GetLength(0)); //choosing random element of array to be added to label
   var label = (Label)Controls["lbl"+i];
   label.Text = randomtext;
}

Or something like that (I can't remember the exact Api for Windows Forms).

On WPF you would use the FindName method instead. The container control has this method.

Most other frameworks should have similar methods. In the worst case you could use reflection:

var label = (Label)this.GetType().GetProperty("lbl" + i).GetValue(this, null);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you for the help, exactly what I was looking for!
I am creating a Windows Form for Desktop
0
    Random r = new Random();

    for (int i = 1; i <= 4; i++)
    {
        int randomtext = r.Next(0, mytext.GetLength(0)); //choosing random element of array to be added to label
        var randomLblIndx = r.Next(1,5);// I am assuming you have 4 lablels only
        var lbl = (Label)this.Controls[lbl + randomLblIndx.ToString()];
        lbl.Text = myarrayOfText[randomtext];
    }

Comments

0

If you're using Windows Forms, there is a property called Controls in each Form (or Component) you create. This property is a collection of type ControlCollection and has a function called Find, which allows you to find for a specific control by it's name.

So, you can do something like this:

    const labelProfix = "lbl";
    Random r = new Random();

    for (int i = 1; i <= 4; i++)
    {
        var randomNumber = r.Next(0, mytext.GetLength(0));
        var label = (Label)Controls.Find(labelPrefix + randomNumber.ToString(), false).First();
        label.Text = "My text";
    }

Note that Find() return an Array (the name can be ambiguous), so we use the extension method First() of System.Linq; to get the first element. Besides, thanks to First(), that line of code will throw an exception if it doesn't find any control with the name we passed, so you can know if code wrong without getting NullReferenceExceptions

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.