1、首先在form1中添加一个新类ComboBoxItem:
public class ComboBoxItem
{
private string _text=null;
private object _value=null;
public string Text{get{return this._text;} set{this._text=value;}}
public object Value{get {return this._value;} set{this._value=value;}}
public override string ToString()
{
return this._text;
}
}
2、在Form1_Load函数中添加:
ComboBoxItem newitem = new ComboBoxItem();
newitem.Text = "abc";
newitem.Value = "1";
comboBox1.Items.Add(newitem);
3、在comboBox1_SelectedIndexChanged中添加:
ComboBoxItem myItem = (ComboBoxItem)comboBox1.Items[comboBox1.SelectedIndex];
MessageBox.Show(myItem.Value.ToString());
就可以看到输出效果了!!!
博客介绍了在WinForm的form1中添加新类ComboBoxItem的代码,还展示了在Form1_Load函数中添加ComboBoxItem对象到comboBox1,以及在comboBox1_SelectedIndexChanged中获取选中项值并输出的代码,帮助读者了解相关操作。
1984





