.net 如何创建带有标签和值的 Winforms 组合框?

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

How to create a Winforms Combobox with Label and Value?

.netwinformscombobox

提问by nokturnal

I am mostly an ASP.NET developer but I am working on a WinForms app and noticed a large difference between the ASP.NET combobox (html select) and that of WinForms. I found (maybe incorrectly so) that WinForm's combobox only has a "label" whereas ASP.NET allows you to specify a "label" and a "value".

我主要是一名 ASP.NET 开发人员,但我正在开发一个 WinForms 应用程序,并注意到 ASP.NET 组合框(html 选择)和 WinForms 组合框之间的巨大差异。我发现(可能是错误的)WinForm 的组合框只有一个“标签”,而 ASP.NET 允许您指定一个“标签”和一个“值”。

I am looking to use a WinForms combobox (or another comparable control) with a label and a value (Foobar / 42329). Is this possible? I have attempted to search for the answer but haven't come up with much. If there is no way to accomplish that, how does one go ahead and design a WinForm combobox that would represent say Cities with their associated database id?

我希望使用带有标签和值 (Foobar / 42329) 的 WinForms 组合框(或其他类似控件)。这可能吗?我试图寻找答案,但没有想出太多。如果没有办法做到这一点,那么如何继续设计一个 WinForm 组合框来表示具有关联数据库 ID 的城市?

  • Toronto / 2324
  • Montreal / 64547
  • Vancouver / 1213
  • 多伦多 / 2324
  • 蒙特利尔 / 64547
  • 温哥华 / 1213

回答by Marc Gravell

I can think of a few ways:

我可以想到几个方法:

  • override the ToString()of a Cityclass to return Name + " / " + Id;
  • ditto, but with a TypeConverter
  • add a DisplayTextproperty that return the same, and use DisplayMember
  • build a shim class for the data
  • 覆盖ToString()一个City类的return Name + " / " + Id;
  • 同上,但有一个 TypeConverter
  • 添加DisplayText返回相同的属性,并使用DisplayMember
  • 为数据构建一个 shim 类

For the last:

最后:

var data = cities.Select(city => new {
     Id = city.Id, Text = city.Name + " / " + city.Id }).ToList();
cbo.ValueMember = "Id";
cbo.DisplayMember = "Text";
cbo.DataSource = data;

回答by Jamie Ide

Assuming that your values are unique, you can first populate a dictionary then bind the combobox to the dictionary. Unfortunately databinding requires an IList or an IListSource so you have to wrap it in Binding Source. I found the solution here.

假设您的值是唯一的,您可以先填充字典,然后将组合框绑定到字典。不幸的是,数据绑定需要 IList 或 IListSource,因此您必须将其包装在 Binding Source 中。我在这里找到了解决方案。

    private void PopulateComboBox()
    {
        var dict = new Dictionary<int, string>();
        dict.Add(2324, "Toronto");
        dict.Add(64547, "Vancouver");
        dict.Add(42329, "Foobar");

        comboBox1.DataSource = new BindingSource(dict, null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";
    }

回答by nokturnal

You could try creating a small class called ComboBoxItem, like so:

您可以尝试创建一个名为 的小类ComboBoxItem,如下所示:

public class ComboBoxItem<T>
{
    public string Label { get; set; }
    public T Value { get; set; }

    public override string ToString()
    {
        return Label ?? string.Empty;
    }
}

And then when you need to get an object, just cast it to a ComboBoxItem.

然后当你需要获取一个对象时,只需将它转换为ComboBoxItem.

回答by SLaks

A ComboBox can be bound to a collection of objects by setting its DataSourceproperty.

ComboBox 可以通过设置其DataSource属性来绑定到一组对象。

By default, the SelectedValue property will be give the object that was selected, and the list will call ToStringon each object and display the result.
However, if you set the DisplayMemberproperty of the ComboBox, it will display the value of the property named in DisplayMember in the list. Similarly, you can set the ValueMemberproperty of the ComboBox and the SelectedValue proeprty will return the value of the property named by ValueMember.

默认情况下,SelectedValue 属性将给出选择的对象,列表将调用ToString每个对象并显示结果。
但是,如果设置ComboBox的DisplayMember属性,它将在列表中显示 DisplayMember 中命名的属性的值。同样,您可以设置ComboBox的ValueMember属性,SelectedValue 属性将返回由 ValueMember 命名的属性的值。



Therefore, you need to bind the ComboBox to a list of objects that have Nameand Valueproperties.
You can then set the ComboBox's [DisplayMemberproperty to Nameand ValueMemberproperty to Value.

因此,您需要将 ComboBox 绑定到具有NameValue属性的对象列表。
然后,您可以将 ComboBox 的 [DisplayMember属性设置为,Name并将ValueMember属性设置为Value

EDIT: You can also call the Addmethod and give it such an object instead of databinding. Alternatively, you could bind it to a List<T>or an array.

编辑:您也可以调用该Add方法并为其提供这样的对象而不是数据绑定。或者,您可以将其绑定到 aList<T>或数组。

回答by CSharpAtl

There is a property called DisplayMember= name of property whose value will be used for display, and ValueMemberwhich is the property of the to use for the value.

有一个名为DisplayMember= name of property 的属性,其值将用于显示,ValueMember这是用于该值的属性。

回答by Matricore

        anestezi.DisplayMember = "Text";
        anestezi.ValueMember = "Value";
        anestezi.DataSource = new[] { 
            new { Text = "Genel", Value = "G" }, 
            new { Text = "Lokal", Value = "L" },
            new { Text = "Sedasyon", Value = "S" }
        };
        anestezi.SelectedIndex = 0;