Propertygrid UIEditor disabling value editing through Keyboard
Asked Answered
C

1

2

I have a propertygrid that uses UITypeEditor to display a Listbox and select a item. This item gets returned on the proertygrid on selection. But how do I disable editing of the selected item directly, readonly option prevents it from getting edited.

Let's say I'm changing a Property Country from another form which displays a list of countries in a listbox. When I select 'Algeria' and press OK, 'Algeria' is displayed across Country, but I can directly edit Algeria to any other value, defeating the purpose of a List to choose from.

Caught answered 11/8, 2011 at 5:29 Comment(1)
Hi, I have the same problem. please let me know if you have found a suitable solution.Baier
C
7

If you just want to show a drop-down list of values to select from, but prevent the user from typing anything into the property value within the PropertyGrid control, you can derive from TypeConverter and override the GetStandardValuesExclusive method to simply return true.

To provide the set of allowable values to show in the drop-down you need to override the GetStandardValuesSupported method to return true then override GetStandardValues to return the list of allowable values you want to show in the drop-down list.

Once you have that in place, you just need to specify your custom type converter on the property like so:

public class MyTypeConverter : TypeConverter
{
  //Override GetStandardValuesExclusive, 
  //GetStandardValues and GetStandardValuesSupported
}

public class SomeClass
{

   [TypeConverter(typeof(MyTypeConverter))]
   public string SomePropertry
   {
      ...
   }
}
Calpac answered 4/9, 2011 at 16:0 Comment(1)
My custom converter was inheriting from StringConverter instead of TypeConverter. Seems like even if you follow the steps above, the user will be able to type in the PropertyGrid unless you inherit specifically from TypeConverter instead of StringConverter. I switched to using TypeConverter and it all worked fine.Retriever

© 2022 - 2024 — McMap. All rights reserved.