Populating a ComboBox using C#

后端 未结 10 1395
不知归路
不知归路 2020-12-02 15:55

I would like to populate a combobox with the following:

Visible item / Item Value

English / En

Italian / It

Spainish / Sp 

etc...         


        
10条回答
  •  北海茫月
    2020-12-02 16:24

      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.

提交回复
热议问题