Search code examples
c#comboboxtostringtypedescriptor

Combobox uses TypeConverter instead of toString C#


I have a class with a custom TypeDescriptor to save and restore the data. In my application i use a ComboBox to select objects of this class. To bind the objects to the ComboBox i use the DataSource property of the ComboBox.

After I created the custom TypeDescriptor for my class, the ComboBox uses the TypeDescriptor to display the Text instead of the ToString method of my class.

How can I change the ComboBox to use the ToString method instead of the TypeDescriptor?


Solution

  • By using a wrapper class and populate the ComboBox with that?

    private class ComboItem
    {
        private MyClass theWrappedInstance;
    
        internal ComboItem(MyClass c)
        {
            theWrappedInstance = c;
        }
    
        public override ToString()
        {
            return theWrappedInstance.ToString();
        }
    }