如何在 WinForms 和 C# 中创建下拉菜单 [英] How to create a drop down menu in WinForms and C#

查看:33
本文介绍了如何在 WinForms 和 C# 中创建下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 Visual Studio/WinForms/C#

I am new to using Visual Studio/WinForms/C#

我正在尝试创建一个简单的下拉菜单,其中每个项目都可以有一个值和一个标签.

I am trying to create a simple drop down menu where each item can have a value and a label.

如果我正在创建一个网络应用程序,这就是我在 HTML 中所做的.但是我怎样才能用 C# 和 WinForms 做到这一点?

This is what I would do in HTML if I was creating a web app. But how can I do this with C# and WinForms?

<select>
<option value="0">Please select One</option>
<option value="1">The first Options</option>
<option value="2">The Second Options</option>
<option value="3">The Third Options</option>
</select>

我尝试了 ComboBox,但似乎不允许我添加值和标签,用户仍然可以输入他们想要的任何内容.

I tried ComboBox but it seems I am not allowed to add a value and a label and the user can still type anything they want.

我尝试了 ListBox,但它不允许我同时使用值和标签.

I tried a ListBox but that did not allow me to use value and label as well.

推荐答案

如果你想要一个值和一个标题(标签),创建一个合适的类

If you want a value and a caption (label), create an appropriate class

class ComboItem
{
    public int ID { get; set; }
    public string Text { get; set; }
}

在组合框中,将 DisplayMember 属性设置为 Text,将 ValueMember 属性设置为 ID.

In the ComboBox you then set the DisplayMember property to Text and the ValueMember property to ID.

ComboBox 的 DropDownStyle 决定了它的行为.DropDownStyle.DropDown 使用户能够输入文本.使用 DropDownStyle.DropDownList 用户只能从列表中选择项目.

The DropDownStyle of the ComboBox determines its behavior. DropDownStyle.DropDown enables the user to type in text. With DropDownStyle.DropDownList the user can only select items from the list.

您可以像这样填充ComboBox:

myCombo.DataSource = new ComboItem[] {
    new ComboItem{ ID = 1, Text = "One" },
    new ComboItem{ ID = 2, Text = "Two" },
    new ComboItem{ ID = 3, Text = "Three" }
};

DataSource 可以是任何类型的可枚举.

The DataSource can be any kind of enumerable.

您可以像这样检索选定的 ID

You can retrieve the selected ID like this

int id = (int)myComboBox.SelectedValue;

<小时>

请注意,您可以向组合框添加任何类型的项目.如果不指定DisplayMemberValueMember 属性,ComboBox 使用对象的ToString 方法来确定显示的文本,您可以通过 SelectedItem 属性检索选定的项目(未选定的值).


Note that you can add any type of item to the ComboBox. If you don't specify the DisplayMember and ValueMember properties, the ComboBox uses the ToString method of the object to determine the text displayed and you can retrieve the selected item (not selected value) through the SelectedItem property.

如果你添加这种类型的对象......

If you add objects of this type ...

class Person
{
    public int PersonID { get; set }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override string ToString()
    {
        return FirstName + " " + LastName;
    }
 }

...到组合框,您可以像这样检索所选项目

...to the ComboBox, you can retrieve the selected item like this

Person selectedPerson = (Person)myComboBox.SelectedItem;
int personID = selectedPerson.PersonID;

ComboBox 将显示人员的名字和姓氏.

The ComboBox will display the first and last names of the persons.

这篇关于如何在 WinForms 和 C# 中创建下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆