gpt4 book ai didi

c# - 获取组合框的选定值

转载 作者:IT王子 更新时间:2023-10-29 04:04:48 25 4
gpt4 key购买 nike

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,但不确定原因。文本似乎显示得很好。

最佳答案

试试这个:

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));
}

关于c# - 获取组合框的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6901070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com