lundi 21 janvier 2019

C# Loop through List of names and display names on a different label each time it loops

I am creating this Random Draw application. For example, User enters in 6 names, they then hit the randomize button and it displays the names randomly in labels beside each other. So for example name at index[0] will play name at index[3] and so on. The problem I am having is I'm not sure how to loop through and display each name on a different label. Is there a way to count my labels and loop through that way? Here is my code for looping through the names in the List and randomly picking one to display on the first label.

for(int i = 0; i < names.Count; i++)
        {
            Random rand = new Random();
            int index = rand.Next(names.Count);
            var name = names[index];

            lblFirstName.Text = name;
            lblFirstName.Visible = true;
            names.RemoveAt(index);
        }

As you can see the name will always be displayed on lblFirstName. I have 5 more labels named lblSecondName etc..




Aucun commentaire:

Enregistrer un commentaire