将组合框选择分配给绑定列表

时间:2013-06-03 14:56:11

标签: c#

我的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;
        }
    }

我已经尝试了很多方法将梳子盒选择分配给最大玩家和玩家,玩家等......但是还没有找到让它工作的方法。有没有人有任何想法?提前谢谢你。

1 个答案:

答案 0 :(得分:0)

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

样品:

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

    HareAndTortoiseGame.NumberOfPlayers = (int)(comboBox.SelectedItem);        
}