Populating a ComboBox using C#
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;
Create a class Language
public class Language
{
public string Name{get;set;}
public string Value{get;set;}
public override string ToString() { return this.Name;}
}
Then, add as many language to the combobox that you want:
yourCombobox.Items.Add(new Language{Name="English",Value="En"});
Set the ValueMember
/DisplayMember
properties to the name of the properties of your Language
objects.
class Language
{
string text;
string value;
public string Text
{
get
{
return text;
}
}
public string Value
{
get
{
return value;
}
}
public Language(string text, string value)
{
this.text = text;
this.value = value;
}
}
...
combo.DisplayMember= "Text";
combo.ValueMember = "Value";
combo.Items.Add(new Language("English", "en"));