使用C#填充ComboBox

时间:2010-03-10 15:11:16

标签: c# .net winforms combobox

我想用以下内容填充组合框:

可见项目/项目值

English / En

Italian / It

Spainish / Sp 

etc....

请帮忙吗?

也可以在填充Combobox后,使其只读?

10 个答案:

答案 0 :(得分:65)

定义一个类

public class Language
{
     public string Name { get; set; }
     public string Value { get; set; }
}

...然后

//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;

答案 1 :(得分:17)

创建一个类语言

public class Language
{
     public string Name{get;set;}
     public string Value{get;set;}
     public override string ToString() { return this.Name;}
}

然后,在所需的组合框中添加尽可能多的语言:

yourCombobox.Items.Add(new Language{Name="English",Value="En"});

答案 2 :(得分:5)

ValueMember / DisplayMember属性设置为Language个对象的属性名称。

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"));

答案 3 :(得分:4)

要使其成为只读,DropDownStyle属性为DropDownStyle.DropDownList。

要填充ComboBox,您需要有一个像Language这样的对象,例如:

public class Language {
    public string Name { get; set; }
    public string Code { get; set; }
}

然后,您可以将IList绑定到ComboBox.DataSource属性,如下所示:

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";

这将完全符合您的预期。

答案 4 :(得分: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;
        }
    }

通过使您的下拉框“只读”我假设您想要阻止用户键入其他选项而不是完全只读用户无法选择值?

如果您希望它完全只读,则可以将enabled属性设置为false。

答案 5 :(得分:2)

你可以做的是创建一个新的类,类似于@Gregoire的例子,但是,你想要覆盖ToString()方法,使它在组合框中正确显示,例如。

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;
    }
}

答案 6 :(得分:2)

简单的方法是:

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";

答案 7 :(得分:1)

如果您只想添加它而不创建新类,请尝试以下方法:

// 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;
}

我建议使用您支持的所有语言的xml文件 那样你就不必依赖c# 我肯定会像上面的程序员建议的那样创建一个语言课程。

答案 8 :(得分:1)

无需使用特定的语言,

只需将其替换为:

KeyValuePair<string,string>

答案 9 :(得分:0)

但你不只是得到你的组合框名称然后items.add("")吗?

例如

Language.Items.Add("Italian");
Language.Items.Add("English");
Language.Items.Add("Spanish");

希望这有帮助:D