C# 将组合框选择指定给绑定列表

C# 将组合框选择指定给绑定列表,c#,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

我在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;
        }
    }
名称空间共享名称类{
/// 
///玩一个叫兔子和乌龟的游戏
/// 
公共级HareAndTortoiseGame{
私人董事会;
公共董事会{
得到{
返回板;
}
}
私人模具die1、die2;
//BindingList就像一个可以增长和收缩的数组。
// 
//使用BindingList将更容易实现带有DataGridView的GUI
私有BindingList播放器=新建BindingList();
公共绑定列表玩家{
得到{
返回球员;
}
}
//最小和最大玩家数。
私人const int MIN_PLAYERS=2;
公共常数int MAX_PLAYERS=6;
private int numberOfPlayers=2;//值2纯粹是为了避免编译器错误。
公共整数玩家{
得到{
返回玩家数量;
}
设置{
玩家数量=价值;
}
}

我已经尝试了很多方法将组合框选择分配给max Player和Player、Player等。但是还没有找到一种方法使其工作。有人有任何想法吗?提前谢谢。

在您的
组合框1\u SelectedIndexChanged
中,您可以尝试解析组合框
SelectedItem
属性

样本:

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

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

您可能希望用“winforms”或“wpf”标记此邮件以吸引更多的专业知识。不清楚您使用的是哪一个。谢谢,但我现在需要为HareAndTortoiseGame.NumberOfPlayers=(int)(comboBox.SelectedItem);这只是使用HareAndTortoiseGame.NumberOfPlayers=(int)创建新变量的情况吗(comboBox.SelectedItem);如果您的事件订阅和您的属性在同一个类中
HareandToToIsegame
,您可以使用
this.NumberOfPlayers=(int)(comboBox.SelectedItem);
。否则,在拥有您的事件的类中,创建一个
HareAndTortoiseGame
的实例:
HareAndTortoiseGame game=new HareAndTortoiseGame();
谢谢您的帮助,我把它整理好了。我没有正确解析。祝您好运。很好,我很高兴能提供帮助。