C# - 从组合框中检索选定的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15083806/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:59:46  来源:igfitidea点击:

C# - retrieve the selected value from a combobox

c#.netwinformsvisual-studio-2010

提问by Leron_says_get_back_Monica

I have a comboBox with ValueMember = IDand DisplayMember = Name. I need the value that is associated with that name so I do something like this:

我有一个带有ValueMember = ID和的组合框DisplayMember = Name。我需要与该名称关联的值,因此我执行以下操作:

if (cboTypeOfMaterial.SelectedIndex != -1)
            {
                string temp = cboTypeOfMaterial.SelectedValue.ToString();
                //More code here...
            }

Which returns the IDvalue as a string. For example - "7".

它将ID值作为字符串返回。例如 - “7”。

If I try :

如果我尝试:

if (cboTypeOfMaterial.SelectedIndex != -1)
                {
                    string temp = cboTypeOfMaterial.DisplayMember.ToString();
                    //More code here...
                }

I get the string Namewhich is the key.

我得到Name了关键的字符串。

And what I need is to get the value of the selected element's Name

我需要的是获取所选元素的值 Name

采纳答案by sa_ddam213

SelectedValuewill return the value of the property defined in ValueMember, SelectedItemwill return the entire object that is selected, if you want to get another value other than your SelectedValueyou will have to cast as the object in your ComboBoxthen you can access your Nameproperty.

SelectedValue将返回 中定义的属性的值ValueMemberSelectedItem将返回所选的整个对象,如果您想获得除您以外的另一个值,您SelectedValue必须将其强制转换为您的对象,ComboBox然后您可以访问您的Name属性。

string temp = (cboTypeOfMaterial.SelectedItem as YourObjectType).Name;

回答by dutzu

Try to access the element via SelectedItemwhich will give you the whole object associated with that entry and then you can access the properties you need, in your case ID.

尝试访问元素,通过SelectedItem它会给你与该条目关联的整个对象,然后你可以访问你需要的属性,在你的情况下ID

回答by Idrees Khan

string temp = cboTypeOfMaterial.ValueMember.ToString();

回答by Tomtom

What you can do is to create a custom class for the entries in the comboBox. This can look like:

您可以做的是为组合框中的条目创建自定义类。这看起来像:

public class ComboBoxItem
{
    public string Display { get; set; }
    public int Id { get; set; }
    public override string ToString()
    {
        return this.Display;
    }
}

Then you can get the selected ComboBoxItem through the following code:

然后就可以通过如下代码获取选中的ComboBoxItem:

ComboBoxItem cbi = (ComboBoxItem)cboTypeOfMaterial.SelectedValue;
if(cbi != null)
   // Access the Property you need

回答by mehdi.loa

I think you can also use the Text property but it is not a nice solution. the better solution is what @dutzu is suggested.

我认为您也可以使用 Text 属性,但这不是一个很好的解决方案。更好的解决方案是@dutzu 的建议。

string temp = cboTypeOfMaterial.Text;

回答by Jim Simson

I know this is an old question, but I'm surprised no one has mentioned:

我知道这是一个老问题,但我很惊讶没有人提到:

ComboBox1.GetItemText(ComboBox1.SelectedItem)

which returns the text representation of the selected item (i.e. the DisplayMember) and is helpful in cases involving a data bound ComboBox, or any ListControlfor that matter.

它返回所选项目(即DisplayMember)的文本表示,并且在涉及数据绑定ComboBox或任何ListControl与此相关的情况下很有帮助。