C# 如何将项目(文本和值)添加到 ComboBox 并在 SelectedIndexChanged 中读取它们(SelectedValue = null)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15780523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How add items(Text & Value) to ComboBox & read them in SelectedIndexChanged (SelectedValue = null)
提问by SilverLight
I want to add some items to a combobox like this :
我想向这样的组合框添加一些项目:
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
ComboboxItem item = new ComboboxItem();
item.Text = "choose a server...";
item.Value = "-1";
ComboBox_Servers.Items.Add(item);
...
and read those texts and values in SelectedIndexChanged
like this :
并SelectedIndexChanged
像这样阅读这些文本和值:
private void ComboBox_Servers_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(ComboBox_Servers.SelectedValue.ToString());
}
But SelectedValue is always null! what is the problem and how can I fix it?
但是 SelectedValue 始终为空!有什么问题,我该如何解决?
采纳答案by Adil
You can take the SelectedItem
and cast it back to your class
and access its properties
.
您可以获取SelectedItem
并将其投射回您的class
并访问其properties
.
MessageBox.Show(((ComboboxItem)ComboBox_Countries_In_Silvers.SelectedItem).Value);
EditYou can try using DataTextField and DataValueField, I used it with DataSource.
编辑您可以尝试使用 DataTextField 和 DataValueField,我将它与 DataSource 一起使用。
ComboBox_Servers.DataTextField = "Text";
ComboBox_Servers.DataValueField = "Value";
回答by Arie
try this:
尝试这个:
ComboBox cbx = new ComboBox();
cbx.DisplayMember = "Text";
cbx.ValueMember = "Value";
EDIT(a little explanation, sory, I also didn't notice your combobox wasn't bound, I blame the lack of caffeine):
编辑(稍微解释一下,抱歉,我也没有注意到您的组合框未绑定,我归咎于缺乏咖啡因):
The difference between SelectedValue and SelectedItem are explained pretty well here: ComboBox SelectedItem vs SelectedValue
SelectedValue 和 SelectedItem 之间的区别在这里解释得很好:ComboBox SelectedItem vs SelectedValue
So, if your combobox is not bound to datasource, DisplayMember and ValueMember doesn't do anything, and SelectedValue will always be null, SelectedValueChanged won't be called. So either bind your combobox:
因此,如果您的组合框未绑定到数据源,DisplayMember 和 ValueMember 不会执行任何操作,并且 SelectedValue 将始终为 null,则不会调用 SelectedValueChanged。所以要么绑定你的组合框:
comboBox1.DisplayMember = "Text";
comboBox1.ValueMember = "Value";
List<ComboboxItem> list = new List<ComboboxItem>();
ComboboxItem item = new ComboboxItem();
item.Text = "choose a server...";
item.Value = "-1";
list.Add(item);
item = new ComboboxItem();
item.Text = "S1";
item.Value = "1";
list.Add(item);
item = new ComboboxItem();
item.Text = "S2";
item.Value = "2";
list.Add(item);
cbx.DataSource = list; // bind combobox to a datasource
or use SelectedItem property:
或使用 SelectedItem 属性:
if (cbx.SelectedItem != null)
Console.WriteLine("ITEM: "+comboBox1.SelectedItem.ToString());
回答by clinetmannar
Dictionary<int,string> comboSource = new Dictionary<int,string>();
comboSource.Add(1, "Sunday");
comboSource.Add(2, "Monday");
Aftr adding values to Dictionary
, use this as combobox
datasource:
在向 中添加值后Dictionary
,将其用作combobox
数据源:
comboBox1.DataSource = new BindingSource(comboSource, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
回答by Coolbeck
combo1.DisplayMember = "Text";
combo1.ValueMember = "Value";
combo1.Items.Add(new { Text = "someText"), Value = "someValue") });
dynamic item = combo1.Items[combo1.SelectedIndex];
var itemValue = item.Value;
var itemText = item.Text;
Unfortunatly "combo1.SelectedValue" does not work, i did not want to bind my combobox with any source, so i came up with this solution. Maybe it will help someone.
不幸的是“combo1.SelectedValue”不起作用,我不想将我的组合框与任何来源绑定,所以我想出了这个解决方案。也许它会帮助某人。
回答by Alex Smith
This is similar to some of the other answers, but is compact and avoids the conversion to dictionary if you already have a list.
这与其他一些答案类似,但很紧凑,如果您已经有一个列表,则可以避免转换为字典。
Given a ComboBox
"combobox" on a windows form and a class SomeClass
with the string
type property Name
,
给定一个ComboBox
windows 窗体上的“组合框”和一个SomeClass
具有string
type 属性的类Name
,
List<SomeClass> list = new List<SomeClass>();
combobox.DisplayMember = "Name";
combobox.DataSource = list;
Which means that combobox.SelectedItem
is a SomeClass
object from list
, and each item in combobox
will be displayed using its property Name
.
这意味着它combobox.SelectedItem
是一个SomeClass
对象 from list
,并且combobox
将使用其属性显示中的每个项目Name
。
You can read the selected item using
您可以使用阅读所选项目
SomeClass someClass = (SomeClass)combobox.SelectedItem;