Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a drop down menu in WinForms and 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.

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.

like image 501
Jaylen Avatar asked Dec 31 '14 16:12

Jaylen


People also ask

How do I create a dropdown in WinForms?

For creating a dropdown in controller use the selectlistitem in get method. And same you need to paas in post method too. List<SelectListItem> items = new List<SelectListItem>(); items. Add(new SelectListItem { Text = "car", Value = "car" }); ViewBag.


2 Answers

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

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

like image 168
Olivier Jacot-Descombes Avatar answered Oct 19 '22 01:10

Olivier Jacot-Descombes


You need to set a datasource for your Combobox, it's better if you create a class and pass a list of Objects, for example:

private void Init()
{
    List<Item> items = new List<Item>();
    items.Add(new Item() { Text = "displayText1", Value = "ValueText1" });
    items.Add(new Item() { Text = "displayText2", Value = "ValueText2" });
    items.Add(new Item() { Text = "displayText3", Value = "ValueText3" });

    comboBox1.DataSource = items;
    comboBox1.DisplayMember = "Text";
    comboBox1.ValueMember = "Value";

}

public class Item
{
    public Item() { }

    public string Value { set; get; }
    public string Text { set; get; }
}

Put the Init() method in your FormName_Load(object sender, EventArgs e){}.

like image 27
Juan Ruiz de Castilla Avatar answered Oct 19 '22 00:10

Juan Ruiz de Castilla