Boh*_*ohn 189 c# combobox winforms
在C#WinApp中,如何将Text和Value添加到我的ComboBox的项目中?我做了一个搜索,通常答案是使用"绑定到源"..但在我的情况下,我的程序中没有准备好绑定源...我怎么能这样做:
combo1.Item[1] = "DisplayText";
combo1.Item[1].Value = "useful Value"
Run Code Online (Sandbox Code Playgroud)
Ada*_*itz 343
您必须创建自己的类类型并覆盖ToString()方法以返回所需的文本.以下是您可以使用的类的简单示例:
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是其用法的简单示例:
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());
}
Run Code Online (Sandbox Code Playgroud)
fab*_*fab 179
// Bind combobox to 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;
Run Code Online (Sandbox Code Playgroud)
buh*_*tla 116
你可以像这样使用匿名类:
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" });
Run Code Online (Sandbox Code Playgroud)
更新:虽然上面的代码将正确显示在组合框中,但您将无法使用SelectedValue或SelectedText属性ComboBox.为了能够使用它们,绑定组合框如下:
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;
Run Code Online (Sandbox Code Playgroud)
小智 30
您应该使用dynamicobject在运行时解析组合框项目.
comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";
comboBox.Items.Add(new { Text = "Text", Value = "Value" });
(comboBox.SelectedItem as dynamic).Value
Run Code Online (Sandbox Code Playgroud)
小智 15
您可以使用DictionaryObject而不是创建自定义类来添加文本和值Combobox.
在Dictionary对象中添加键和值:
Dictionary<string, string> comboSource = new Dictionary<string, string>();
comboSource.Add("1", "Sunday");
comboSource.Add("2", "Monday");
Run Code Online (Sandbox Code Playgroud)
将源Dictionary对象绑定到Combobox:
comboBox1.DataSource = new BindingSource(comboSource, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
Run Code Online (Sandbox Code Playgroud)
检索键和值:
string key = ((KeyValuePair<string,string>)comboBox1.SelectedItem).Key;
string value = ((KeyValuePair<string,string>)comboBox1.SelectedItem).Value;
Run Code Online (Sandbox Code Playgroud)
完整来源:Combobox文本和价值
Amr*_*rhy 14
这是我想到的方式之一:
combo1.Items.Add(new ListItem("Text", "Value"))
要更改项目的文本或值,您可以这样做:
combo1.Items[0].Text = 'new Text';
combo1.Items[0].Value = 'new Value';
Run Code Online (Sandbox Code Playgroud)
Windows窗体中没有名为ListItem的类.它只存在于ASP.NET中,所以在使用之前你需要编写自己的类,就像@Adam Markowitz在他的回答中所做的那样.
还要检查这些页面,它们可能有所帮助:
小智 11
不知道这是否适用于原帖中给出的情况(更不用说这是两年后的事实),但这个例子对我有用:
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";
Run Code Online (Sandbox Code Playgroud)
要重新读取您的值,您必须将SelectedItem属性强制转换为DictionaryEntry对象,然后您可以评估它的Key和Value属性.例如:
DictionaryEntry deImgType = (DictionaryEntry)cmbImageType.SelectedItem;
MessageBox.Show(deImgType.Key + ": " + deImgType.Value);
Run Code Online (Sandbox Code Playgroud)
//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);
Run Code Online (Sandbox Code Playgroud)
如果有人对此仍然感兴趣,这里有一个简单而灵活的类,用于带有文本和任何类型值的组合框项(与 Adam Markowitz 的示例非常相似):
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;
}
}
Run Code Online (Sandbox Code Playgroud)
使用 the<T>比将 the声明value为 an更好object,因为这样object您就必须跟踪您用于每个项目的类型,并将其转换到您的代码中以正确使用它。
我已经在我的项目中使用它有一段时间了。这真的很方便。
| 归档时间: |
|
| 查看次数: |
792144 次 |
| 最近记录: |