Home > Net >  How to evenly move items in main list box into 2 separate list boxes
How to evenly move items in main list box into 2 separate list boxes

Time:08-18

Hey guys so I am creating a simple app that generates random teams from the names that the user enters into the text box. Once the user enters all the names they click the "Generate" button and I want the names from the main list box to separate randomly and evenly into the left side "RED Team" and the right side "BLUE team". Here is how the UI looks.

enter image description here

So far I have figures out how to add them into either side of the screen however not how to do it evenly or randomly. Here is the Code I have set up so far:

public partial class MainWindow : Window
{
    //Number of players added
    public int numPlayers = 0;


    public MainWindow()
    {
        InitializeComponent();
    }

    //Enter Name into list box for players
    public void TextBoxEnter_Click(object sender, KeyEventArgs e)
    {
        try
        {
            //Check if user pressed enter in text box
            if (e.Key == Key.Return)
            {
                //convert text into string and add to list itme box
                nameListBox.Items.Add(enterNameTxtBox.Text.ToString());
                //Clear text box so user can re enter name
                enterNameTxtBox.Clear();
                //Increase num players
                numPlayers  ;
                //Add to number of players value label
                numPlayersLabel.Content = numPlayers;

            }
        }
        catch(Exception ex) //Check for errors
        {
            MessageBox.Show(ex.ToString());
        }
       
    }
    public void GenerateBtn_Click(object sender, RoutedEventArgs e)
    {
        //Need to get all items in listbox and randomly generate them to red or blue team evenly 

        //Random number
        //Random rnd = new Random();
        //int num = rnd.Next(1, nameListBox.Items.Count);

        //Loop through items and add them evenly to red and blue side
        for(int i = 0; i < nameListBox.Items.Count; i  )
        {
            redTeamListBox.Items.Add(nameListBox.Items[i].ToString());

            blueTeamListBox.Items.Add(nameListBox.Items[i].ToString());

        }


    }

    public void DeleteName_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            //Delete selcted name on screen
            nameListBox.Items.Remove(nameListBox.SelectedItem);          
            //Refresh the display view to show new list
            nameListBox.Items.Refresh();
            //Remove num players
            numPlayers--;
            //Update label view 
            numPlayersLabel.Content = numPlayers;
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    

}

the "GenerateBtn_Click" is where I handle the event to separate the names in the "ENTER NAME" Box shown above.

Any suggestions or help would be greatly appreciated! Thanks in advance

CodePudding user response:

Try it like this

    public void GenerateBtn_Click(object sender, RoutedEventArgs e)
    {
        Random rnd = new Random();
        
        //Loop through items and add them evenly to red and blue side
        while(nameListBox.Items.Count > 0)
        {
            int i = rnd.Next(0, nameListBox.Items.Count);
            if (redTeamListBox.Items.Count < blueTeamListBox.Items.Count)
            {
                redTeamListBox.Items.Add(nameListBox.Items[i].ToString());
            } else {
                blueTeamListBox.Items.Add(nameListBox.Items[i].ToString());
            }
            nameListBox.Items.RemoveAt(i);
        }
    }
  • Related