views:

45

answers:

3

Hello guys,

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

Your help on this would be much appreciated :)

-- Ruby

+1  A: 

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.

Dan Tao
I agree and it was exactly how I implemented it before. But my requirements forces me to retrive the value of "Code" when I do comboBox1.SelectedValue not the formatted string.
Ruby
@Ruby: See Jakob's answer; I think that provides exactly the solution you're looking for.
Dan Tao
A: 

this could be because u r using ValueMember. use DisplayMember Property, add another property on the Map class in the get of this property return the formatted string.

Vinay B R
+3  A: 

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, i.e.:

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