C#: Bug in ComboBox when items are UserControls?
A minimum working example displaying empty strings:
public class TestControl : UserControl {
public override string toString(){
return "Example";
}
}
...
combobox.Items.Add(new TestControl());
...
When I call
combobox.Items.Add(new TestControl().ToString()开发者_如何转开发);
directly, the entry is "Example".
Is this a bug in the ComboBox control or am I doing something wrong? Thank you
Odd, that should work. Another alternative would be to set the DisplayMember property of the combobox to a property on your TestControl:
Typically, the representation of an object in the ComboBox is the string returned by that object's ToString method. If you want to have a member of the object displayed instead, choose the member that will be displayed by setting the DisplayMember property to the name of the appropriate member.
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx (in the remarks section)
精彩评论