简体   繁体   English

C#combobox重写了ToString

[英]C# combobox overridden ToString

I am experiencing some problems while working with ComboBox. 我在使用ComboBox时遇到了一些问题。

The display member for my combobox is not being populated by the overridden ToString method of class MAP. 我的组合框的显示成员没有被类MAP的重写ToString方法填充。

Here is my code: 这是我的代码:

Form1.cs: Form1.cs中:

private void Form1_Load(object sender, EventArgs e) {  
    ...  
    ...      
    MAPList MAP = new MAPList();  
    comboBox1.DataSource = MAP.All;  
    comboBox1.ValueMember = "Code";  
    ...  
    ...  
}

MAPList.cs: MAPList.cs:

public class MAPList {  
    public readonly List<MAP> All;

    public MAPList() {
        All = new List<MAP>();

        var MapData = // Getting map data

        foreach(MAP m in MapData) {
            All.Add(new Map(m.Name, m.Code));
        }
    }
}

MAP.cs: MAP.cs:

public class MAP {
    public readonly string Name;

    private string code;
    public string Code { get { return code; } }

    public RadioCode(string Name, string Code) {
        this.Name = Name;
        this.code = Code;
    }

    public override string ToString() {
        return String.Format("{0}: {1}", Name, Code);
    }
}

ToString will not be called if you set ValueMember . 如果设置ValueMember则不会调用ToString。 If you do not set ValueMember it will work as expected but then of course Code will not be used as the selected value of the ComboBox. 如果您没有设置ValueMember ,它将按预期工作,但当然Code不会被用作ComboBox的选定值。

Alternatively, if you wish to use ValueMember you may also want to set DisplayMember . 或者,如果您希望使用ValueMember您可能还需要设置DisplayMember You can create a property on your MAP that is used for display, ie: 您可以在MAP上创建用于显示的属性,即:

public class MAP
{
    public readonly string Name;

    private string code;

    public string Code { get { return code; } }
    public string Display { get { return ToString(); } }

    public MAP(string Name, string Code)
    {
        this.Name = Name;
        this.code = Code;
    }

    public override string ToString()
    {
        return String.Format("{0}: {1}", Name, Code);
    }
}

In the form you can then set DisplayMember : 然后,您可以在表单中设置DisplayMember

MAPList MAP = new MAPList();
comboBox1.DataSource = MAP.All;
comboBox1.ValueMember = "Code";
comboBox1.DisplayMember = "Display";

This is because you've set your ValueMember property to "Code", so the values in the combobox are not your Map objects but rather the strings corresponding to their Code properties. 这是因为您已将ValueMember属性设置为“Code”,因此组合框中的值不是Map对象,而是与其Code属性对应的字符串。

If you remove this line: 如果删除此行:

comboBox1.ValueMember = "Code";

...it will work as you expect. ......它会像你期望的那样工作。

If you want the ComboBox to display its items according to your Map type's ToString method, then Jakob's answer is right on: create a property on your Map type that provides a string formatted exactly how you want it, and set the DisplayMember property of the ComboBox to the name of this property. 如果你想让ComboBox根据你的Map类型的ToString方法显示它的项目,那么Jakob的答案是正确的:在你的Map类型上创建一个属性,提供一个完全符合你想要的格式的字符串,并设置ComboBox的DisplayMember属性到这个属性的名称。

I know this is an old post, but if someone wants to use ToString() without creating a property to just call ToString(), you'll have to explicitly set the DisplayMember value to an empty string like this: 我知道这是一个旧帖子,但是如果有人想要使用ToString()而不创建只调用ToString()的属性,则必须将DisplayMember值显式设置为空字符串,如下所示:

Form1.cs: Form1.cs中:

private void Form1_Load(object sender, EventArgs e) {  
    ...  
    ...      
    MAPList MAP = new MAPList();  
    comboBox1.DataSource = MAP.All;  
    comboBox1.ValueMember = "Code"; 
    comboBox1.DisplayMember = "";  // Explicitly set it to an empty String
    ...  
    ...  
}

this could be because ur using ValueMember. 这可能是因为你使用的是ValueMember。 use DisplayMember Property, add another property on the Map class in the get of this property return the formatted string. 使用DisplayMember属性,在获取此属性的Map类中添加另一个属性,返回格式化的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM