在C# WinApp中,我该如何将文本和值同时添加到ComboBox的项中? 我进行了搜索,通常的答案都是使用“绑定到源代码”...但在我的情况下,我的程序中没有准备好的绑定源... 我应该如何实现这样的功能:
combo1.Item[1] = "DisplayText";
combo1.Item[1].Value = "useful Value"
你需要创建自己的类并重写ToString()方法来返回你想要的文本。这是一个可以使用的简单示例类:
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
下面是其使用的简单示例: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());
}
// Bind combobox to a dictionary.
Dictionary<string, string> test = new Dictionary<string, string>();
test.Add("1", "dfdfdf");
test.Add("2", "dfdfdf");
test.Add("3", "dfdfdf");
comboBox1.DataSource = new BindingSource(test, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
// Get combobox selection (in handler)
string value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;
您可以像这样使用匿名类:
comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";
comboBox.Items.Add(new { Text = "report A", Value = "reportA" });
comboBox.Items.Add(new { Text = "report B", Value = "reportB" });
comboBox.Items.Add(new { Text = "report C", Value = "reportC" });
comboBox.Items.Add(new { Text = "report D", Value = "reportD" });
comboBox.Items.Add(new { Text = "report E", Value = "reportE" });
更新: 尽管上面的代码可以在组合框中正确显示,但您将无法使用 ComboBox
的 SelectedValue
或 SelectedText
属性。 为了能够使用这些属性,请按以下方式绑定组合框:
comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";
var items = new[] {
new { Text = "report A", Value = "reportA" },
new { Text = "report B", Value = "reportB" },
new { Text = "report C", Value = "reportC" },
new { Text = "report D", Value = "reportD" },
new { Text = "report E", Value = "reportE" }
};
comboBox.DataSource = items;
List<Object> items = new List<Object>();
然后我能够在循环中使用方法 items.Add( new { Text = "report A", Value = "reportA" } );
。 - AndrewcomboBox.SelectedItem.GetType().GetProperty("Value").GetValue(comboBox.SelectedItem, null)
- Optavius您应该使用dynamic
对象在运行时解析组合框项目。
comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";
comboBox.Items.Add(new { Text = "Text", Value = "Value" });
(comboBox.SelectedItem as dynamic).Value
您可以使用 Dictionary
对象来代替创建自定义类,用于向 Combobox
添加文本和值。
在 Dictionary
对象中添加键和值:
Dictionary<string, string> comboSource = new Dictionary<string, string>();
comboSource.Add("1", "Sunday");
comboSource.Add("2", "Monday");
Dictionary
对象绑定到Combobox
:comboBox1.DataSource = new BindingSource(comboSource, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
获取键和值:
string key = ((KeyValuePair<string,string>)comboBox1.SelectedItem).Key;
string value = ((KeyValuePair<string,string>)comboBox1.SelectedItem).Value;
完整源代码:ComboBox 文本和值
这是我想到的一种方法:
combo1.Items.Add(new ListItem("文本", "值"))
如果要更改项目的文本或值,您可以这样做:
combo1.Items[0].Text = 'new Text';
combo1.Items[0].Value = 'new Value';
Windows Forms 中并没有名为 ListItem 的类,它只存在于 ASP.NET 中,因此在使用之前您需要编写自己的类,就像 @Adam Markowitz 在 他的答案 中所做的那样。
此外,请查看以下页面,它们可能会有所帮助:
我不知道这个方法是否适用于原帖中提到的情况(更不用说现在已经是两年后了),但是这个示例对我有效:
Hashtable htImageTypes = new Hashtable();
htImageTypes.Add("JPEG", "*.jpg");
htImageTypes.Add("GIF", "*.gif");
htImageTypes.Add("BMP", "*.bmp");
foreach (DictionaryEntry ImageType in htImageTypes)
{
cmbImageType.Items.Add(ImageType);
}
cmbImageType.DisplayMember = "key";
cmbImageType.ValueMember = "value";
为了读取您的值,您需要将SelectedItem属性转换为DictionaryEntry对象,然后可以评估该对象的Key和Value属性。例如:DictionaryEntry deImgType = (DictionaryEntry)cmbImageType.SelectedItem;
MessageBox.Show(deImgType.Key + ": " + deImgType.Value);
//set
comboBox1.DisplayMember = "Value";
//to add
comboBox1.Items.Add(new KeyValuePair("2", "This text is displayed"));
//to access the 'tag' property
string tag = ((KeyValuePair< string, string >)comboBox1.SelectedItem).Key;
MessageBox.Show(tag);
public class ComboBoxItem<T>
{
public string Name;
public T value = default(T);
public ComboBoxItem(string Name, T value)
{
this.Name = Name;
this.value = value;
}
public override string ToString()
{
return Name;
}
}
使用<T>
比将value
声明为object
更好,因为使用object
时,您需要跟踪每个项目使用的类型,并在代码中对其进行转换才能正确使用。
我在我的项目中使用它已有一段时间了,非常方便。
Dictionary<int, string> userListDictionary = new Dictionary<int, string>();
foreach (var user in users)
{
userListDictionary.Add(user.Id,user.Name);
}
cmbUser.DataSource = new BindingSource(userListDictionary, null);
cmbUser.DisplayMember = "Value";
cmbUser.ValueMember = "Key";
获取数据
MessageBox.Show(cmbUser.SelectedValue.ToString());
comboBox1.Items.Add(new ComboboxItem {Text = "项目文本1", Value = 12 });
- Martin