Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating a ComboBox using C#

I would like to populate a combobox with the following:

Visible item / Item Value

English / En  Italian / It  Spainish / Sp   etc.... 

Any help please?

Also it is possible that after populating the Combobox, to make it read only?

like image 872
mouthpiec Avatar asked Mar 10 '10 15:03

mouthpiec


People also ask

What is ComboBox in C sharp?

C# ComboBox is a combination of a TextBox and a ListBox control. Only one list item is displayed at one time in a ComboBox and other available items are loaded in a drop down list.


1 Answers

Define a class

public class Language {      public string Name { get; set; }      public string Value { get; set; } } 

then...

//Build a list var dataSource = new List<Language>(); dataSource.Add(new Language() { Name = "blah", Value = "blah" }); dataSource.Add(new Language() { Name = "blah", Value = "blah" }); dataSource.Add(new Language() { Name = "blah", Value = "blah" });  //Setup data binding this.comboBox1.DataSource = dataSource; this.comboBox1.DisplayMember = "Name"; this.comboBox1.ValueMember = "Value";  // make it readonly this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; 
like image 146
Allen Rice Avatar answered Sep 21 '22 15:09

Allen Rice