8

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!

l33t
  • 18,692
  • 16
  • 103
  • 180

2 Answers2

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 2
    Inheriting `System.ComponentModel.StringConverter` solved the problem. Obviously, text editing cannot be done with other types than strings. Thanks for the links though! – l33t Mar 20 '12 at 16:33
7

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 :)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Zlatko
  • 71
  • 1
  • 1
  • 2
    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`. – Uwe Keim Dec 19 '14 at 10:45