simple way to remove properties in propertyGrid

  • Thread starter Thread starter michelqa
  • Start date Start date
M

michelqa

Hi,

I have a form with a control..(a button for example). Then the
control properties are displayed in a propertyGrid control.

Is there any simple way to remove some unwanted properties from the
propertyGrid....removing the "AccessibleName" properties by example?

Any example about how to play with the PropertyDescriptorCollection to
remove some properties??


// remove AccessibleName properties from Button properties...
//...
PropertyGrid1.SelectedObject =button1;
//...


Thanks
 
The simplest way would be to re-declare the property, setting
[Browsable(false)] on the property; example below.

There are other ways you could do it (in increasing complexity):

Use the BrowsableAttributes property of the grid to limit visibility
(very crude).

Look at ICustomTypeDescriptor / TypeDescriptionProvided for providing
custom metadata for the control, omitting these ones.

Write a custom tab for the property-grid that omits them.

The first is *by far* the simplest, even if it is more work - but note
that this will affect the regular IDE as well as your PropertyGrid.

Marc

== redeclare example ==

using System;
using System.ComponentModel;
using System.Windows.Forms;

class MyControl : Control
{
[DefaultValue("")]
[Localizable(true)]
[Browsable(false)]
public new string AccessibleName
{
get { return base.AccessibleName; }
set { base.AccessibleName = value; }
}

}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
using (Form f = new Form())
using (MyControl ctl = new MyControl())
using (PropertyGrid grid = new PropertyGrid())
{
ctl.Dock = DockStyle.Top;
grid.Dock = DockStyle.Fill;
grid.SelectedObject = ctl;
f.Controls.Add(grid);
f.Controls.Add(ctl);
Application.Run(f);
}
}
}
 
Thanks!...

Your example was really usefull, Now I figure out how to add new
"custom properties and remove unwanted properties into an existing
control.

When changing a property value, the value is displayed in bold...even
if its modified in the code...
by example in your code you change ctl.Dock (ctl.Dock=DockStyle.Top)
so Dock value is displayed in bold in the property grid... is there
any way to prevent this?

thanks again for your time.
 
Again, it depends. If you own the property, you can do things like
specifying a [DefaultValue(...)], or providing a "bool
ShouldSerializeDock()" method (a special convention recognised by the
system) to disable it - but ultimately it doesn't distinguish between
reasons, it just cares that it isn't the default value.

I suspect in this case re-declaring the Foo property and adding a
ShouldSerialiseFoo would suffice, but you don't necessarily want to do
this for every property!

If you are using the ICustomTypeDescriptor/TypeDescriptionProvider
approach you can also override SouldSerializeValue on a
PropertyDescriptor basis; I often use this approach to track changes
since the object was last committed (i.e. the pending changes) - but it
helps that I have a bespoke utility library for doing this... it isn't
something you'd want to redo from scratch regularly!

Mac
 
Back
Top