获取下拉框的选定值

47
public class ComboboxItem { 
            public string Text { get; set; } 
            public string Value { get; set; }
            public override string ToString() { return Text; } 
        }

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = comboBox1.SelectedIndex;
            int selecteVal = (int)comboBox1.SelectedValue; 
            ComboboxItem selectedCar = (ComboboxItem)comboBox1.SelectedItem;
            MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));
        }
我正在像这样添加它们:
ComboboxItem item = new ComboboxItem();
                    item.Text = cd.Name;
                    item.Value = cd.ID;
                    this.comboBox1.Items.Add(item);

我一直收到一个NullReferenceExeption异常,不确定原因。看起来文本显示得很好。

7个回答

52

试试这个:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox cmb = (ComboBox)sender;
    int selectedIndex = cmb.SelectedIndex;
    int selectedValue = (int)cmb.SelectedValue;

    ComboboxItem selectedCar = (ComboboxItem)cmb.SelectedItem;
    MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));        
}

8
我认为ComboBoxItem类仅适用于WPF项目。 - T30
OP在他们的帖子中声明了它。惊讶的是System.Windows.Forms命名空间中没有这样的东西...还是我错过了什么? - AS7K

15

你之所以会收到 NullReferenceException 异常,是因为你正在使用空值的 cmb.SelectedValue。目前 comboBox 不知道你的自定义类 ComboboxItem 的值是什么,因此你需要采取以下措施:

ComboboxItem selectedCar = (ComboboxItem)comboBox2.SelectedItem;
int selecteVal = Convert.ToInt32(selectedCar.Value);

更好的方法是使用数据绑定,例如:

ComboboxItem item1 = new ComboboxItem();
item1.Text = "test";
item1.Value = "123";

ComboboxItem item2 = new ComboboxItem();
item2.Text = "test2";
item2.Value = "456";

List<ComboboxItem> items = new List<ComboboxItem> { item1, item2 };

this.comboBox1.DisplayMember = "Text";
this.comboBox1.ValueMember = "Value";
this.comboBox1.DataSource = items;

有没有办法在不使用数据源的情况下,对自定义项目使用comboBox.SelectedValue呢?例如,如果使用数据源,则无法从combobox 项目中删除或添加项目。 - user3532232

7
我遇到了类似的错误,我的类是:
public class ServerInfo
{
    public string Text { get; set; }
    public string Value { get; set; }
    public string PortNo { get; set; }

    public override string ToString()
    {
        return Text;
    }
}

但是我所做的是,将我的类转换为ComboBox的SelectedItem属性。因此,我将拥有所选项目的所有类属性。

// Code above
ServerInfo emailServer = (ServerInfo)cbServerName.SelectedItem;

mailClient.ServerName = emailServer.Value;
mailClient.ServerPort = emailServer.PortNo;

希望这对某人有所帮助!干杯!


1

你需要将所选项目转换为你的自定义类(ComboboxItem)。尝试这样做:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cmb = (ComboBox)sender;
            int selectedIndex = cmb.SelectedIndex;
            string selectedText = this.comboBox1.Text;
            string selectedValue = ((ComboboxItem)cmb.SelectedItem).Value.ToString();

ComboboxItem selectedCar = (ComboboxItem)cmb.SelectedItem;
MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));        

}


0
你遇到的问题是无法将 SelectedValue 转换为整数。这是主要问题,因此使用以下代码片段将有所帮助:
int selectedValue;
bool parseOK = Int32.TryParse(cmb.SelectedValue.ToString(), out selectedValue);

0

试试这个:

private void cmbLineColor_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataRowView drv = (DataRowView)cmbLineColor.SelectedItem;
        int selectedValue = (int)drv.Row.ItemArray[1];
    }

0

试试这个:

int selectedIndex = comboBox1.SelectedIndex;
comboBox1.SelectedItem.ToString();
int selectedValue = (int)comboBox1.Items[selectedIndex];

OP有一个C#的问题。你的答案看起来像是C++。 - Adrian W

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接