Hi,
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?
Hi,
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?
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"));
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"});
Language[] items = new Language[]{new Language("English", "En"),
new Language("Italian", "It")};
languagesCombo.ValueMember = "Alias";
languagesCombo.DisplayMember = "FullName";
languagesCombo.DataSource = items.ToList();
languagesCombo.DropDownStyle = ComboBoxStyle.DropDownList;
class Language
{
public string FullName { get; set; }
public string Alias { get; set; }
public Language(string fullName, string alias)
{
this.FullName = fullName;
this.Alias = alias;
}
}
By making your drop down box "read-only" I am assuming you want to prevent user's typing in other options as opposed to being fully read-only where users cannot select a value??
If you wanted it to be fully read-only you could set the enabled property to be false.
To make it read-only, the DropDownStyle property to DropDownStyle.DropDownList.
To populate the ComboBox, you will need to have a object like Language or so containing both for instance:
public class Language {
public string Name { get; set; }
public string Code { get; set; }
}
Then, you may bind a IList to your ComboBox.DataSource property like so:
IList<Language> languages = new List<Language>();
languages.Add(new Language("English", "en"));
languages.Add(new Language("French", "fr"));
ComboxBox.DataSource = languages;
ComboBox.DisplayMember = "Name";
ComboBox.ValueMember = "Code";
This will do exactly what you expect.
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;
What you could do is create a new class, similar to @Gregoire's example, however, you would want to override the ToString()
method so it appears correctly in the combo box e.g.
public class Language
{
private string _name;
private string _code;
public Language(string name, string code)
{
_name = name;
_code = code;
}
public string Name { get { return _name; } }
public string Code { get { return _code; } }
public override void ToString()
{
return _name;
}
}