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

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

问题描述

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

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



如果我正在创建一个网络应用程序,这是我将要做的HTML。但是如何使用C#和WinForms来执行此操作?

 < select> 
< option value =0>请选择One< / option>
< option value =1>第一个Options< / option>
< option value =2>第二个选项< / option>
< option value =3>第三个选项< / option>
< / select>

我尝试过ComboBox,但似乎我不允许添加一个值和一个标签,用户可以仍然输入任何他们想要的东西。



我尝试过一个ListBox,但这并不允许我使用价值和标签。

解决方案

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

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

在ComboBox中,然后设置 DisplayMember 属性为文本 ValueMember 属性为 ID






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






您可以这样填写 ComboBox

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

数据源可以是任何类型的枚举。



您可以检索这样的选择的ID

  int id =(int)myComboBox.SelectedValue; 






请注意,您可以添加任何类型的项目ComboBox。如果您没有指定 DisplayMember ValueMember 属性,ComboBox将使用 ToString 方法,以确定显示的文本,您可以通过 SelectedItem 属性来检索所选项目(不是选定的值)。



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

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

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

...到ComboBox,你可以发现选择这样的项目

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

ComboBox将显示人物的姓和名。


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

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

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>

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.

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; }
}

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


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.


You can fill the ComboBox like this:

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

The `DataSource can be any kind of enumerable.

You can retrieve the selected ID like this

int id = (int)myComboBox.SelectedValue;


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 in order 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 retrive the selected item like this

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

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

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

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