How to filter properties in PropertyGrid?

I need simple control designer in runtime. I want set only several properties for several controls. For example Text for Label control and Size form my UserControl. I plan use PropertyGrid for this purpose. But it displays all browsable properties for every control. I need to set list of properties which have to be displayed in PropertyGrid. I try to create class MyPropertyGrid derived from PropertyGrid and class MyPropertiesTab derived from PropertiesTab. I override MyPropertyGrid.DefaultTabType to typeof(MyPropertiesTab) and create empty constructor w/o parameters.

But when I set MyPropertyGrid.SelectedObject to any control I get exception:

System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.PropertyGrid.UpdatePropertiesViewTabVisibility()
at System.Windows.Forms.PropertyGrid.ShowEventsButton(Boolean value)
at System.Windows.Forms.PropertyGrid.set_SelectedObjects(Object[] value)
at System.Windows.Forms.PropertyGrid.set_SelectedObject(Object value)

I don't write any code in MyPropertiesTab class (except empty constructor w/o parameters).

If I remove overriden MyPropertyGrid.DefaultTabType then MyPropertyGrid is displayed.

What is wrong

How to make MyPropertiesTab working



Answer this question

How to filter properties in PropertyGrid?

  • Justin Souders

    But I can not apply TypeConverter to existing Microsoft's controls...
  • RVNL

    I'm sorry it took me so long to respond. This article about custom type descriptors might also be of interest to you. Let me know what you think.

  • Just-A-Nerd

    If you'd like to filter out properties, try writing your own TypeConverter. By overriding GetProperties, you can supply the properties you need to the propertygrid.

    I made my own typeconverter. Here's how I implemented the GetProperties, maybe you can retrieve some ideas from it. It is an object similar to the Padding structure, but for floats. I expose some specific properties in it.



    public override PropertyDescriptorCollection GetProperties( ITypeDescriptorContext context, object value, Attribute[] attributes )
    {
    PropertyDescriptorCollection collection = TypeDescriptor.GetProperties( value, attributes );
    string[] textArray = new string[ 5 ] { "All", "Left", "Top", "Right", "Bottom" };
    return collection.Sort( textArray );
    }



  • How to filter properties in PropertyGrid?