37

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?

Jim Fell
  • 13,750
  • 36
  • 127
  • 202
mouthpiec
  • 3,923
  • 18
  • 54
  • 73
  • Possible duplicate of [How to bind a List to a ComboBox?](https://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox) – KyleMit Apr 26 '19 at 22:21

10 Answers10

80

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;
Allen Rice
  • 19,068
  • 14
  • 83
  • 115
17

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"});
Gregoire
  • 24,219
  • 6
  • 46
  • 73
7

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";
Tiago S
  • 1,299
  • 23
  • 23
5

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"));
thelost
  • 6,638
  • 4
  • 28
  • 44
5

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.

Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
3
  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.

MarkB29
  • 4,752
  • 4
  • 21
  • 15
  • @MarkB29: Enabled = false will prevent the user from even entering to the ComboBox to select the language she/he wants. This is definitely not a good thing. I guess mouthpiec wishes the user to be able to select her/his language, though Enabled = false will not allow it. To set a ComboBox read-only, you need to set its DropDownStyle property to DropDownStyle.DropDownList. – Will Marcouiller Mar 10 '10 at 15:53
  • @Will Marcouiller: I have actually put this in my answer. – MarkB29 Mar 10 '10 at 16:09
  • Yes, indeed. Please excuse my misunderstanding, but after your answer's code sample, you specify to set the Enabled property to false to make it fully readonly, which in my point of view disabling a ComboBox doesn't render any advantage, as a simple label would do the job. That is what I meant. Perhaps am I missing something of your intention. – Will Marcouiller Mar 10 '10 at 18:05
2

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;
    }
}
James
  • 80,725
  • 18
  • 167
  • 237
1

No need to use a particular class Language,

Just replace it with :

KeyValuePair<string,string>
Aelios
  • 11,849
  • 2
  • 36
  • 54
1

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

Opal
  • 81,889
  • 28
  • 189
  • 210
Zyak
  • 21
  • 3
1

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.

Dinah
  • 52,922
  • 30
  • 133
  • 149
hidden
  • 3,216
  • 8
  • 47
  • 69