C #: error in ComboBox when elements are UserControls?


I am adding objects from classes that are derived from UserControl to a ComboBox control. This is really useful since I can access the desired control directly from the drop-down list.
All this works fine, except that all ComboBox entries are empty strings (the derived UserControls behind it are fully accessible using selectedItem) ...
ComboBox uses DropDownList as a drop-down style, but changing it does not eliminate it.

Minimal working example showing blank lines:

public class TestControl : UserControl { public override string toString(){ return "Example"; } } ... combobox.Items.Add(new TestControl()); ... 

When i call

 combobox.Items.Add(new TestControl().ToString()); 

record "Example".

Is this a bug in the ComboBox control, or am I doing something wrong? thank you

+1
source share
1 answer

Odd, this should work. Another alternative would be to set the DisplayMember property in the property list box in TestControl:

Typically, the representation of an object in a ComboBox is the string returned by this ToString object. If you want a member of the object displayed instead, select the member that will be displayed by setting the DisplayMember property to the name of the corresponding member.

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx (in the comments section)

+3
source

All Articles