tags:

views:

828

answers:

6

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?

+5  A: 

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
this wont work, you must use properties for this, not pure variable
Fredou
@Fredou: I fixed that. Thank you!
thelost
+6  A: 

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
@Gregoire this won't display "English" as the display text in the combo box.
James
@James: corrected, thanks
Gregoire
+1 But I´d wrote public override string ToString() { return this.Name + " / " + this.Value;} instead.
Javier Morillo
Sorry, I hadn´t understood the question well, your answer was correct
Javier Morillo
+3  A: 
  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
@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
@Will Marcouiller: I have actually put this in my answer.
MarkB29
@MarkB29: 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
+3  A: 

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
+1  A: 

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
+1  A: 

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