Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 318
l33t Avatar asked Mar 20 '12 15:03

l33t


People also ask

How do I make my ComboBox non editable?

To make the text portion of a ComboBox non-editable, set the DropDownStyle property to "DropDownList". The ComboBox is now essentially select-only for the user. You can do this in the Visual Studio designer, or in C# like this: stateComboBox.


2 Answers

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.

like image 181
Reed Copsey Avatar answered Oct 24 '22 01:10

Reed Copsey


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

like image 25
Zlatko Avatar answered Oct 24 '22 01:10

Zlatko