XAML 如何将主列表框中的项目均匀地移动到两个单独的列表框中

tnkciper  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(129)

嘿,伙计们,我正在创建一个简单的应用程序,从用户输入到文本框中的名字生成随机的团队。一旦用户输入所有的名字,他们点击“生成”按钮,我希望主列表框中的名字随机和均匀地分为左侧“红色团队”和右侧“蓝色团队”。这是UI的外观。

到目前为止,我已经知道如何将它们添加到屏幕的任意一边,但还没有知道如何均匀或随机地添加。以下是我目前为止设置的代码:

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

    

}

“GenerateBtn_Click”是我处理事件以分隔上面所示的“ENTER NAME”框中的名称的地方。
任何建议或帮助将不胜感激!提前感谢

vcirk6k6

vcirk6k61#

这样试试看

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

相关问题