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 in which each value can have a value and a label.

This is what I would do in HTML if I created a web application. 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 to me that I am not allowed to add a value and a label, and the user can still enter whatever he wants.

I tried ListBox, but that also did not allow me to use the value and label.

+6
source share
5 answers

If you need a value and a label (label), create the appropriate class

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

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


DropDownStyle ComboBox defines its behavior. DropDownStyle.DropDown allows the user to enter text. Using DropDownStyle.DropDownList user can only select items from a list.


You can populate the ComboBox as follows:

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

DataSource can be any enumerated.

You can get the selected identifier, for example,

 int id = (int)myComboBox.SelectedValue; 

Please note that you can add any type of item to the ComboBox. If you do not specify the DisplayMember and ValueMember , the ComboBox uses the object's ToString method to determine the displayed text, and you can get the selected item (not the 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; } } 

... in ComboBox, you can get the selected item like this

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

ComboBox will display the names and surnames of individuals.

+11
source

It seems that the value is just a reference to which element is selected, right? Then you can use the combobox index, making it a lot easier.

Not sure if your objects are known before the build, if so, just add them to the constructor, combobox properties. If not, then you can add them dynamically by simply doing:

  List<string> items = new List<string>() { "item1", "item2" }; comboBox1.DataSource = items; 

And find out which item is selected:

  int index = comboBox1.SelectedIndex; 
+3
source

You need to install a data source for your Combobox, it is 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; } } 

Place the Init() method on FormName_Load(object sender, EventArgs e){} .

+2
source

The ComboBox displays the result of returning from the ToString call, so you can define a display class that wraps the value and displays the text and adds it to its dropdown line.

I.e:

  public class ItemDisplay<TValue> { private readonly string m_displayText; public ItemDisplay(TValue value, String displayText) { this.Value = value; m_displayText = displayText; } public TValue Value { get; set; } public override string ToString() { return m_displayText; } } 

and add items to your combo box:

  comboBox1.Items.Add(new ItemDisplay<int>(1, "FirstValue")); comboBox1.Items.Add(new ItemDisplay<int>(2, "Second")); comboBox1.Items.Add(new ItemDisplay<int>(3, "Third")); 
0
source

To create a drop-down list in the controller, use selectlistitem in the get method. And the same thing you need for paas in the post method.

  List<SelectListItem> items = new List<SelectListItem>(); items.Add(new SelectListItem { Text = "car", Value = "car" }); ViewBag.List = new SelectList(items, "Text", "Value"); 

In view, you need to go through the drop-down list.

  @Html.DropDownList("option", (ViewBag.List as SelectList), "Select", new { @style ="padding:5.5px;margin-bottom:8px;margin-right:-5px;" }) 

`

0
source

Source: https://habr.com/ru/post/980374/


All Articles