44
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,但不知道为什么。文本似乎显示得很好。

4

7 回答 7

48

试试这个:

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));        
}
于 2011-08-01T16:00:32.347 回答
15

你得到NullReferenceExeption是因为你使用的cmb.SelectedValue是 null。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;
于 2011-08-01T16:24:44.517 回答
6

我有一个类似的错误,我的班级是

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;

我希望这可以帮助别人!干杯!

于 2014-02-13T21:22:37.387 回答
0

试试这个:

private void cmbLineColor_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataRowView drv = (DataRowView)cmbLineColor.SelectedItem;
        int selectedValue = (int)drv.Row.ItemArray[1];
    }
于 2015-05-19T11:12:59.537 回答
0

您遇到的问题SelectedValue是没有转换为整数。这是主要问题,因此使用以下代码片段将对您有所帮助:

int selectedValue;
bool parseOK = Int32.TryParse(cmb.SelectedValue.ToString(), out selectedValue);
于 2016-07-28T17:20:32.940 回答
0

试试这个:

int selectedIndex = comboBox1.SelectedIndex;
comboBox1.SelectedItem.ToString();
int selectedValue = (int)comboBox1.Items[selectedIndex];
于 2018-11-10T14:23:43.897 回答
0

您必须将所选项目转换为您的自定义类(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));        

}

于 2020-04-05T14:35:09.690 回答