Search code examples
c#.netwinformscombobox

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?


Solution

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