How do I add an editable combobox to a System.Windows.Forms.PropertyGrid?
Asked Answered
A

2

9

I have a System.Windows.Forms.PropertyGrid with different types of values. For a specific item, I want to show a list of useful values to choose from. The user may also type a new value. Something similar to a traditional dropdown combobox:

enter image description here

So far, I have my own System.ComponentModel.TypeConverter, but I can't figure out how to get both the dropdown with suggested values and the possibility to edit the value directly. Please help!

Antrorse answered 20/3, 2012 at 15:50 Comment(0)
W
8

You can accomplish this by implementing your own UITypeEditor.

I recommend reading Getting the Most Out of the .NET Framework PropertyGrid Control. In particular, the section titled Providing a Custom UI for Your Properties walks through how to make a custom control for a specific property.

Walton answered 20/3, 2012 at 15:55 Comment(1)
Inheriting System.ComponentModel.StringConverter solved the problem. Obviously, text editing cannot be done with other types than strings. Thanks for the links though!Antrorse
T
8

It is easy. In your own StringConverter return false for GetStandardValuesExclusive and that is it.

Look here:

internal class cmbKutoviNagiba : StringConverter
{
      public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
      {
          return FALSE;    // <----- just highlight! remember to write it lowecase
      }

      public override TypeConverter.StandardValuesCollection GetStandardValues(
          ITypeDescriptorContext context)
      {
          string[] a = { "0", "15", "30", "45", "60", "75", "90" };
          return new StandardValuesCollection(a);
      }

      public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
      {
          return true;
      }
  }

I wrote FALSE in capital letters, just to make you easyer to see it. Please put it in small letters :)

Theoretical answered 12/9, 2012 at 0:51 Comment(1)
BTW: The override of GetStandardValuesExclusive seems only to get called when used in a class that derives from StringConverter. It seems to not get called when you derive your class from TypeConverter.Padraig

© 2022 - 2024 — McMap. All rights reserved.