开发者

Binding the selected value from a combobox to a member of a class

开发者 https://www.devze.com 2022-12-16 23:35 出处:网络
I have a combobox that is bound to a an instance of开发者_开发知识库 a class.I need to get the user\'s selection ID of the combobox and set a class property equal to it.

I have a combobox that is bound to a an instance of开发者_开发知识库 a class. I need to get the user's selection ID of the combobox and set a class property equal to it.

For example, here is the class:

public class robot
{
    private string _ID;
    private string _name;
    private string _configFile;
    [XmlElement("hardware")]
    public hardware[] hardware;

    public string ID
    {
        get { return _ID; }
        set { _ID = value; }
    }
    public string name
    {
        get { return _name; }
        set { _name = value; }
    }
    public string configFile
    {
        get { return _configFile; }
        set { _configFile = value; }
    }
}

Now here is the code to bind the combobox to an instance of that class. This display's the name of each robot in the array in the combobox.

    private void SetupDevicesComboBox()
    {
        robot[] robot = CommConfig.robot;
        cmbDevices.DataSource = robot;
        cmbDevices.DisplayMember = "name";
        cmbDevices.ValueMember = "ID";
    }

But now I can't seem to take what the user selects and use it. How do I use the "ID" of what the user select's from the combobox?

Settings.selectedRobotID = cmbDevices.ValueMember;  //This just generates "ID" regardless of what is selected.

I also tried

Settings.selectedRobotID = cmbDevices.SelectedItem.ToString();  //This just generates "CommConfig.robot"


Try

Settings.selectedRobotID = ((robot)cmbDevices.SelectedItem).ID;
0

精彩评论

暂无评论...
验证码 换一张
取 消