简体   繁体   中英

C# combobox overridden ToString

I am experiencing some problems while working with ComboBox.

The display member for my combobox is not being populated by the overridden ToString method of class MAP.

Here is my code:

Form1.cs:

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

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:

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 . 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.

Alternatively, if you wish to use ValueMember you may also want to set DisplayMember . You can create a property on your MAP that is used for display, ie:

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 :

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.

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.

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:

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. use DisplayMember Property, add another property on the Map class in the get of this property return the formatted string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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