C#中如何为ComboBox添加key-value对
You must create your own class type and override the ToString() method to return the text you want. Here is a simple example of a class you can use:
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
The following is a simple example of its usage:
private void Test()
{
ComboboxItem item = new ComboboxItem();
item.Text = "Item text1";
item.Value = 12;
comboBox1.Items.Add(item);
comboBox1.SelectedIndex = 0;
MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}
本文介绍了一种在C#中为ComboBox添加key-value对的方法。通过创建自定义类ComboboxItem并覆盖ToString()方法来实现。示例展示了如何使用此类向ComboBox添加带有文本和值的项。

1256

被折叠的 条评论
为什么被折叠?



