vendredi 12 février 2021

How to loop through a list box and display to a different list box? [duplicate]

Hi this is my assignment that I've been working on and I can't seem to find the solution. I have a student list box that contains names that are randomized between two different groups. My issue is getting the student names to the different groups. Here's my code:

        try
        {
            //clear groups
            group1ListBox.Items.Clear();
            group1ListBox.Items.Clear();

            while (studentsList.Items.Count > 0)
            {
                //declare variables
                string studentName1 = studentsList.Items[0] as string;
                int group;

                Random rand = new Random(); //get random variable

                group = rand.Next(2); //assign random variable

                if (group == 0) //begin if statement to assign names to a group
                {
                    group1ListBox.Items.Add(studentName1);
                }
                else if (group == 1)
                {
                    group2ListBox.Items.Add(studentName1);
                }
                studentsList.Items.Remove(0);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }



Aucun commentaire:

Enregistrer un commentaire