0

我在我的 Windows GUI 上使用以下代码完成了一个组合框:

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox comboBox = (ComboBox)sender;
        if (comboBox.SelectedIndex == 2)
        {
           //players binding list == 2 or in other words, players binding list = comboBox 

        }

当组合框中的数字发生变化时,我想将棋盘上的玩家人数更改为已选择的人数并将它们放回起始方格,问题是我不知道如何更新玩家人数在组合框中选择的数字。

我拥有的另一个决定玩家数量的代码在这里:

namespace SharedGameClasses {
/// <summary>
/// Plays a game called Hare and the Tortoise
/// </summary>
public class HareAndTortoiseGame {


    private Board board;
    public Board Board {
        get {
            return board;
        }
    }

    private Die die1, die2;

    // A BindingList is like an array that can grow and shrink. 
    // 
    // Using a BindingList will make it easier to implement the GUI with a DataGridView
    private BindingList<Player> players = new BindingList<Player>();
    public BindingList<Player> Players {
        get {
            return players;
        }
    }



    // Minimum and maximum players.
    private const int MIN_PLAYERS = 2;
    public const int MAX_PLAYERS = 6;

    private int numberOfPlayers = 2;  // The value 2 is purely to avoid compiler errors.

    public int NumberOfPlayers {
        get {
            return numberOfPlayers;
        }
        set {
            numberOfPlayers = value;
        }
    }

我已经尝试了很多方法将组合框选择分配给最大玩家和玩家、玩家等。但还没有找到让它工作的方法。有没有人有任何想法?先感谢您。

4

1 回答 1

0

在您的内部,您comboBox1_SelectedIndexChanged可以尝试解析组合框SelectedItem属性。

样本:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox comboBox = (ComboBox)sender;

    HareAndTortoiseGame.NumberOfPlayers = (int)(comboBox.SelectedItem);        
}
于 2013-06-03T15:20:59.590 回答