Populating a ComboBox using C#
I would like to populate a combobox with the following:
Visible item / Item Value
English / En
Italian / It
Spainish / S开发者_JS百科p
etc....
Any help please?
Also it is possible that after populating the Combobox, to make it read only?
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"});
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.
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"));
Simple way is:
Dictionary<string, string> dict = new Dictionary<string, string>()
{
{"English ","En" },
{"Italian ","It" },
{"Spainish ","Sp " }
};
combo.DataSource = new BindingSource(dict, null);
combo.DisplayMember = "Key";
combo.ValueMember = "Value";
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.
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;
}
}
If you simply want to add it without creating a new class try this:
// WPF
<ComboBox Name="language" Loaded="language_Loaded" />
// C# code
private void language_Loaded(object sender, RoutedEventArgs e)
{
List<String> language= new List<string>();
language.Add("English");
language.Add("Spanish");
language.Add("ect");
this.chartReviewComboxBox.ItemsSource = language;
}
I suggest an xml file with all your languages that you will support that way you do not have to be dependent on c# I would definitly create a class for languge like the above programmer suggest.
No need to use a particular class Language,
Just replace it with :
KeyValuePair<string,string>
but do you not just get your combo box name and then items.add("")
?
For instance
Language.Items.Add("Italian");
Language.Items.Add("English");
Language.Items.Add("Spanish");
Hope this helped :D
精彩评论