How to hide a object's inherited properties ?

G

Guest

I create a user control object and show its properties through a propertygrid
object in my application. It is sure that there are 100 hundred of properties
inherited from System.Windows.Forms.UserControl and they are all showed on
the propertygrid. Is there a way i can hide all these inherited properties
and show only my user control's properties ?
 
H

Herfried K. Wagner [MVP]

mrVithan said:
I create a user control object and show its properties
through a propertygrid object in my application. It
is sure that there are 100 hundred of properties
inherited from System.Windows.Forms.UserControl
and they are all showed on the propertygrid. Is
there a way i can hide all these inherited properties
and show only my user control's properties ?

You can try to override the property and mark it using the
'BrowsableAttribute' atttribute. If you want to hide the members in code
too, take a look at the 'EditorBrowsableAttribute' too.
 
G

Guest

Thank you for your answer. But i think i need a bit deeper way to do ....
'cause
I have like 10 - 20 UserControls and i have to hide all inhertied properties
from UserControl properties and show only the my created properties for that
control.

If i do in the normal way by overriding the browserable for each inhertied
properties one by one .... i think that would be a huge task to do and it is
a nightmare.

Is there any "Silver Bullet" ?
 
T

Tim Wilson

If i do in the normal way by overriding the browserable for each inhertied
properties one by one .... i think that would be a huge task to do and it is
a nightmare.
AFAIK, this is the only way to do it.
 
B

Bob Powell [MVP]

Overriding properties and adding a Browsable attribute is not an infallible
solution. Some properties aren't virtual so you end up having to replace
properties entirely with the new keyword. I tend to avoid this method if
at-all possible.

Hiding properties from design-time users can be done in one of two ways.
They both essentially use the same mechanisms but one works in runtime
reflection as well.

To hide properties in design time you can create a designer for the object
and use the PreFilterProperties method override of the designer to remove
properties from the list of declared properties provided to the design time
system. Essenially, the PropertyGrid sees a collection of PropertyDescriptor
objects that it displays, removing some of the descriptors hides them from
the property grid.

A more permanent way of hiding or adding properties is to make the object
implement the ICustomPropertyProvider interface. This method enables the
derived class itself to limit or augment the properties seen when reflection
is used to look at an object. The mechanism is similar to that of the
designer but encapsulated in the class itself. Here you can get the list of
properties provided by the base class an remove the ones you don't wish to
see anymore.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

Can anyone show me how to get all properties instance of inherited class ?
^^'' thank you
 
G

Guest

Here is my code i have tried:

AttributeCollection attributes = properties.Attributes;
BrowsableAttribute myAttribute =
(BrowsableAttribute)attributes[typeof(BrowsableAttribute)];
properties.Attributes[typeof(BrowsableAttribute)] =
BrowsableAttribute.Yes;

It said it is read-only !!! ... Can i reset the browsableattributes in this
way or i miss something ?

To Bob: Can you show me some atricle doing what you said ?
 
J

John Saunders

....
A more permanent way of hiding or adding properties is to make the object
implement the ICustomPropertyProvider interface. This method enables the
derived class itself to limit or augment the properties seen when
reflection
is used to look at an object. The mechanism is similar to that of the
designer but encapsulated in the class itself. Here you can get the list
of
properties provided by the base class an remove the ones you don't wish to
see anymore.

Bob, I don't find an ICustomPropertyProvider interface. Is that a .NET 2.0
post-beta 1 feature? I do see TypeDescriptionProvider in 2.0.

John
 
B

Bob Powell [MVP]

Oops, sorry thats my fault. working late on a saturday night, fresh batch of
Beaujolais Nouveau, you know how that is...

The interface is ICustomTypeDescriptor and it works like this;

The following application, after my signature, removes a bunch of inherited
properties from it's list. Note how ICustomTypeDescriptor must be fully
implemented, as you would expect. The two property grids essentially show a
Control object, the one on the right has had its properties editied
according to an array of property names held in the class.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RemoveProps
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
private System.Windows.Forms.PropertyGrid propertyGrid2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

this.propertyGrid1.SelectedObject=new Control();

this.propertyGrid2.SelectedObject=new FilteredControl();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.propertyGrid2 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(168, 264);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// propertyGrid2
//
this.propertyGrid2.CommandsVisibleIfAvailable = true;
this.propertyGrid2.LargeButtons = false;
this.propertyGrid2.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid2.Location = new System.Drawing.Point(224, 0);
this.propertyGrid2.Name = "propertyGrid2";
this.propertyGrid2.Size = new System.Drawing.Size(168, 264);
this.propertyGrid2.TabIndex = 0;
this.propertyGrid2.Text = "propertyGrid1";
this.propertyGrid2.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid2.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 266);
this.Controls.Add(this.propertyGrid1);
this.Controls.Add(this.propertyGrid2);
this.Name = "Form1";
this.Text = "Form1";
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_SizeChanged(object sender, System.EventArgs e)
{
this.propertyGrid1.Location=new Point(0,0);
this.propertyGrid1.Size=new
Size(this.ClientRectangle.Width/2,this.ClientRectangle.Height);
this.propertyGrid2.Location=new Point(this.ClientRectangle.Width/2,0);
this.propertyGrid2.Size=new
Size(this.ClientRectangle.Width/2,this.ClientRectangle.Height);
}
}


class FilteredControl : Control, ICustomTypeDescriptor
{

private string[] NamesToRemove={
"AccesibilityObject",
"AccessibleDescription",
"AccessibleName",
"AllowDrop",
"BackgroundImage",
"Capture",
"DisplayRectangle",
"Enabled",
"ForeColor",
"Region",
"Tag",
"Text",
"Visible"};


//Does the property filtering...
private PropertyDescriptorCollection
FilterProperties(PropertyDescriptorCollection pdc)
{
ArrayList toRemove=new ArrayList();
foreach(string s in NamesToRemove)
toRemove.Add(s);

PropertyDescriptorCollection adjustedProps=new
PropertyDescriptorCollection(new PropertyDescriptor[]{});
foreach(PropertyDescriptor pd in pdc)
if(!toRemove.Contains(pd.Name))
adjustedProps.Add(pd);

return adjustedProps;
}



#region ICustomTypeDescriptor Members

public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this,true);
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}

public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}


public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,
attributes, true);
return FilterProperties(pdc);
}

PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,
true);
return FilterProperties(pdc);
}

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}

public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}

public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}

public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}

#endregion

}

}
 
G

Guest

Wow Bob your code work great and i think this would be my solution ... but
let's me make somthing clear

The code that you gave is a way to remove properties of a object that will
show in a propertygird but those properties are still accessable, right ? It
means those property just invisiable on a propertygrid ? but why the designer
seem can't access the removed properties ?

So, what is the different between overriding each property
BrowseableAttribute and this way?

I am asking this question because i have tried to remove some properties;
such as size and location. Well, these properties don't show in the
properties as i wish but the object in designer look weird. I can't resize
the object or change the location in the designer, but i can do set the size
or location in my code. Will it be the same effect if i override "Size" and
"Location" BrowsableAttribute not to remove these properties?

Hope your understand my point.

Bob Powell said:
Oops, sorry thats my fault. working late on a saturday night, fresh batch of
Beaujolais Nouveau, you know how that is...

The interface is ICustomTypeDescriptor and it works like this;

The following application, after my signature, removes a bunch of inherited
properties from it's list. Note how ICustomTypeDescriptor must be fully
implemented, as you would expect. The two property grids essentially show a
Control object, the one on the right has had its properties editied
according to an array of property names held in the class.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RemoveProps
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
private System.Windows.Forms.PropertyGrid propertyGrid2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

this.propertyGrid1.SelectedObject=new Control();

this.propertyGrid2.SelectedObject=new FilteredControl();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.propertyGrid2 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(168, 264);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// propertyGrid2
//
this.propertyGrid2.CommandsVisibleIfAvailable = true;
this.propertyGrid2.LargeButtons = false;
this.propertyGrid2.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid2.Location = new System.Drawing.Point(224, 0);
this.propertyGrid2.Name = "propertyGrid2";
this.propertyGrid2.Size = new System.Drawing.Size(168, 264);
this.propertyGrid2.TabIndex = 0;
this.propertyGrid2.Text = "propertyGrid1";
this.propertyGrid2.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid2.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 266);
this.Controls.Add(this.propertyGrid1);
this.Controls.Add(this.propertyGrid2);
this.Name = "Form1";
this.Text = "Form1";
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_SizeChanged(object sender, System.EventArgs e)
{
this.propertyGrid1.Location=new Point(0,0);
this.propertyGrid1.Size=new
Size(this.ClientRectangle.Width/2,this.ClientRectangle.Height);
this.propertyGrid2.Location=new Point(this.ClientRectangle.Width/2,0);
this.propertyGrid2.Size=new
Size(this.ClientRectangle.Width/2,this.ClientRectangle.Height);
}
}


class FilteredControl : Control, ICustomTypeDescriptor
{

private string[] NamesToRemove={
"AccesibilityObject",
"AccessibleDescription",
"AccessibleName",
"AllowDrop",
"BackgroundImage",
"Capture",
"DisplayRectangle",
"Enabled",
"ForeColor",
"Region",
"Tag",
"Text",
"Visible"};


//Does the property filtering...
private PropertyDescriptorCollection
FilterProperties(PropertyDescriptorCollection pdc)
{
ArrayList toRemove=new ArrayList();
foreach(string s in NamesToRemove)
toRemove.Add(s);

PropertyDescriptorCollection adjustedProps=new
PropertyDescriptorCollection(new PropertyDescriptor[]{});
foreach(PropertyDescriptor pd in pdc)
if(!toRemove.Contains(pd.Name))
adjustedProps.Add(pd);

return adjustedProps;
}



#region ICustomTypeDescriptor Members

public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this,true);
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}

public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}


public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,
attributes, true);
return FilterProperties(pdc);
}

PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,
true);
return FilterProperties(pdc);
}

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}

public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}

public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}

public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}

#endregion

}

}

------------------------------------------------------------------




John Saunders said:
...

Bob, I don't find an ICustomPropertyProvider interface. Is that a .NET 2.0
post-beta 1 feature? I do see TypeDescriptionProvider in 2.0.

John
 
B

Bob Powell [MVP]

This code makes certain properties visible or invisible to reflection.
Reflection is what enables the PropertyGrid to display the properties and
events of an object at design time.

This difference between overriding each property and removing the
PropertyDescriptor from the list of reflected properties is that the
functionality of the actual property doesn't change and the removal or even
the addition of any number of properties can be accomplished in one go.

You're correct in assuming that the properties themselves do not disappear
and are still accessible to code as they always were. The compiler doesn't
need reflection to know what methods, properties, fieldsand events are
available so as far as the code is concerned the properties are still
identical.

Overriding the property and changing the Browsable attribute works in a very
similar way because the TypeDescriptor provides a GetProperties(Attribute[])
method which is used to get properties that only have the specified
attributes. It's laborious though because you have to override each property
and remember to maintain the base-class functionality if you need to.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





mrVithan said:
Wow Bob your code work great and i think this would be my solution ... but
let's me make somthing clear

The code that you gave is a way to remove properties of a object that will
show in a propertygird but those properties are still accessable, right ? It
means those property just invisiable on a propertygrid ? but why the designer
seem can't access the removed properties ?

So, what is the different between overriding each property
BrowseableAttribute and this way?

I am asking this question because i have tried to remove some properties;
such as size and location. Well, these properties don't show in the
properties as i wish but the object in designer look weird. I can't resize
the object or change the location in the designer, but i can do set the size
or location in my code. Will it be the same effect if i override "Size" and
"Location" BrowsableAttribute not to remove these properties?

Hope your understand my point.

Bob Powell said:
Oops, sorry thats my fault. working late on a saturday night, fresh batch of
Beaujolais Nouveau, you know how that is...

The interface is ICustomTypeDescriptor and it works like this;

The following application, after my signature, removes a bunch of inherited
properties from it's list. Note how ICustomTypeDescriptor must be fully
implemented, as you would expect. The two property grids essentially show a
Control object, the one on the right has had its properties editied
according to an array of property names held in the class.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RemoveProps
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
private System.Windows.Forms.PropertyGrid propertyGrid2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

this.propertyGrid1.SelectedObject=new Control();

this.propertyGrid2.SelectedObject=new FilteredControl();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.propertyGrid2 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(168, 264);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// propertyGrid2
//
this.propertyGrid2.CommandsVisibleIfAvailable = true;
this.propertyGrid2.LargeButtons = false;
this.propertyGrid2.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid2.Location = new System.Drawing.Point(224, 0);
this.propertyGrid2.Name = "propertyGrid2";
this.propertyGrid2.Size = new System.Drawing.Size(168, 264);
this.propertyGrid2.TabIndex = 0;
this.propertyGrid2.Text = "propertyGrid1";
this.propertyGrid2.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid2.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 266);
this.Controls.Add(this.propertyGrid1);
this.Controls.Add(this.propertyGrid2);
this.Name = "Form1";
this.Text = "Form1";
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_SizeChanged(object sender, System.EventArgs e)
{
this.propertyGrid1.Location=new Point(0,0);
this.propertyGrid1.Size=new
Size(this.ClientRectangle.Width/2,this.ClientRectangle.Height);
this.propertyGrid2.Location=new Point(this.ClientRectangle.Width/2,0);
this.propertyGrid2.Size=new
Size(this.ClientRectangle.Width/2,this.ClientRectangle.Height);
}
}


class FilteredControl : Control, ICustomTypeDescriptor
{

private string[] NamesToRemove={
"AccesibilityObject",
"AccessibleDescription",
"AccessibleName",
"AllowDrop",
"BackgroundImage",
"Capture",
"DisplayRectangle",
"Enabled",
"ForeColor",
"Region",
"Tag",
"Text",
"Visible"};


//Does the property filtering...
private PropertyDescriptorCollection
FilterProperties(PropertyDescriptorCollection pdc)
{
ArrayList toRemove=new ArrayList();
foreach(string s in NamesToRemove)
toRemove.Add(s);

PropertyDescriptorCollection adjustedProps=new
PropertyDescriptorCollection(new PropertyDescriptor[]{});
foreach(PropertyDescriptor pd in pdc)
if(!toRemove.Contains(pd.Name))
adjustedProps.Add(pd);

return adjustedProps;
}



#region ICustomTypeDescriptor Members

public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this,true);
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}

public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}


public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,
attributes, true);
return FilterProperties(pdc);
}

PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,
true);
return FilterProperties(pdc);
}

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}

public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}

public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}

public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}

#endregion

}

}

------------------------------------------------------------------




John Saunders said:
...
A more permanent way of hiding or adding properties is to make the object
implement the ICustomPropertyProvider interface. This method enables the
derived class itself to limit or augment the properties seen when
reflection
is used to look at an object. The mechanism is similar to that of the
designer but encapsulated in the class itself. Here you can get the list
of
properties provided by the base class an remove the ones you don't
wish
to
see anymore.

Bob, I don't find an ICustomPropertyProvider interface. Is that a .NET 2.0
post-beta 1 feature? I do see TypeDescriptionProvider in 2.0.

John
 
G

Guest

Well then .... i have to thank you so much .... Re-implement the base-case
functionality when i override all properties is a horrible task for me,
especially when i don't know how it does .... ^^

Bob Powell said:
This code makes certain properties visible or invisible to reflection.
Reflection is what enables the PropertyGrid to display the properties and
events of an object at design time.

This difference between overriding each property and removing the
PropertyDescriptor from the list of reflected properties is that the
functionality of the actual property doesn't change and the removal or even
the addition of any number of properties can be accomplished in one go.

You're correct in assuming that the properties themselves do not disappear
and are still accessible to code as they always were. The compiler doesn't
need reflection to know what methods, properties, fieldsand events are
available so as far as the code is concerned the properties are still
identical.

Overriding the property and changing the Browsable attribute works in a very
similar way because the TypeDescriptor provides a GetProperties(Attribute[])
method which is used to get properties that only have the specified
attributes. It's laborious though because you have to override each property
and remember to maintain the base-class functionality if you need to.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





mrVithan said:
Wow Bob your code work great and i think this would be my solution ... but
let's me make somthing clear

The code that you gave is a way to remove properties of a object that will
show in a propertygird but those properties are still accessable, right ? It
means those property just invisiable on a propertygrid ? but why the designer
seem can't access the removed properties ?

So, what is the different between overriding each property
BrowseableAttribute and this way?

I am asking this question because i have tried to remove some properties;
such as size and location. Well, these properties don't show in the
properties as i wish but the object in designer look weird. I can't resize
the object or change the location in the designer, but i can do set the size
or location in my code. Will it be the same effect if i override "Size" and
"Location" BrowsableAttribute not to remove these properties?

Hope your understand my point.

Bob Powell said:
Oops, sorry thats my fault. working late on a saturday night, fresh batch of
Beaujolais Nouveau, you know how that is...

The interface is ICustomTypeDescriptor and it works like this;

The following application, after my signature, removes a bunch of inherited
properties from it's list. Note how ICustomTypeDescriptor must be fully
implemented, as you would expect. The two property grids essentially show a
Control object, the one on the right has had its properties editied
according to an array of property names held in the class.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RemoveProps
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
private System.Windows.Forms.PropertyGrid propertyGrid2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

this.propertyGrid1.SelectedObject=new Control();

this.propertyGrid2.SelectedObject=new FilteredControl();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.propertyGrid2 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(168, 264);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// propertyGrid2
//
this.propertyGrid2.CommandsVisibleIfAvailable = true;
this.propertyGrid2.LargeButtons = false;
this.propertyGrid2.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid2.Location = new System.Drawing.Point(224, 0);
this.propertyGrid2.Name = "propertyGrid2";
this.propertyGrid2.Size = new System.Drawing.Size(168, 264);
this.propertyGrid2.TabIndex = 0;
this.propertyGrid2.Text = "propertyGrid1";
this.propertyGrid2.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid2.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 266);
this.Controls.Add(this.propertyGrid1);
this.Controls.Add(this.propertyGrid2);
this.Name = "Form1";
this.Text = "Form1";
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_SizeChanged(object sender, System.EventArgs e)
{
this.propertyGrid1.Location=new Point(0,0);
this.propertyGrid1.Size=new
Size(this.ClientRectangle.Width/2,this.ClientRectangle.Height);
this.propertyGrid2.Location=new Point(this.ClientRectangle.Width/2,0);
this.propertyGrid2.Size=new
Size(this.ClientRectangle.Width/2,this.ClientRectangle.Height);
}
}


class FilteredControl : Control, ICustomTypeDescriptor
{

private string[] NamesToRemove={
"AccesibilityObject",
"AccessibleDescription",
"AccessibleName",
"AllowDrop",
"BackgroundImage",
"Capture",
"DisplayRectangle",
"Enabled",
"ForeColor",
"Region",
"Tag",
"Text",
"Visible"};


//Does the property filtering...
private PropertyDescriptorCollection
FilterProperties(PropertyDescriptorCollection pdc)
{
ArrayList toRemove=new ArrayList();
foreach(string s in NamesToRemove)
toRemove.Add(s);

PropertyDescriptorCollection adjustedProps=new
PropertyDescriptorCollection(new PropertyDescriptor[]{});
foreach(PropertyDescriptor pd in pdc)
if(!toRemove.Contains(pd.Name))
adjustedProps.Add(pd);

return adjustedProps;
}



#region ICustomTypeDescriptor Members

public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this,true);
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}

public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}


public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,
attributes, true);
return FilterProperties(pdc);
}

PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,
true);
return FilterProperties(pdc);
}

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}

public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}

public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}

public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}

#endregion

}

}

------------------------------------------------------------------




"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
...
A more permanent way of hiding or adding properties is to make the
object
implement the ICustomPropertyProvider interface. This method enables the
derived class itself to limit or augment the properties seen when
reflection
is used to look at an object. The mechanism is similar to that of the
designer but encapsulated in the class itself. Here you can get the list
of
properties provided by the base class an remove the ones you don't wish
to
see anymore.

Bob, I don't find an ICustomPropertyProvider interface. Is that a .NET 2.0
post-beta 1 feature? I do see TypeDescriptionProvider in 2.0.

John
 
G

Guest

There really is no clean way to "rid" of virtual base's classes properties.
In other forum it was suggested to override and throw exception: Propertry
Cannot be Used, but then again, the users can always cast to base class. It
was also suggested that it is OO design that is being broken when this is
attempted.

I would think that easiest way to hide all properties for design
(reflection) purposes is to use prefilter method and remove everything from
the properties dictionary...




mrVithan said:
Well then .... i have to thank you so much .... Re-implement the base-case
functionality when i override all properties is a horrible task for me,
especially when i don't know how it does .... ^^

Bob Powell said:
This code makes certain properties visible or invisible to reflection.
Reflection is what enables the PropertyGrid to display the properties and
events of an object at design time.

This difference between overriding each property and removing the
PropertyDescriptor from the list of reflected properties is that the
functionality of the actual property doesn't change and the removal or even
the addition of any number of properties can be accomplished in one go.

You're correct in assuming that the properties themselves do not disappear
and are still accessible to code as they always were. The compiler doesn't
need reflection to know what methods, properties, fieldsand events are
available so as far as the code is concerned the properties are still
identical.

Overriding the property and changing the Browsable attribute works in a very
similar way because the TypeDescriptor provides a GetProperties(Attribute[])
method which is used to get properties that only have the specified
attributes. It's laborious though because you have to override each property
and remember to maintain the base-class functionality if you need to.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





mrVithan said:
Wow Bob your code work great and i think this would be my solution ... but
let's me make somthing clear

The code that you gave is a way to remove properties of a object that will
show in a propertygird but those properties are still accessable, right ? It
means those property just invisiable on a propertygrid ? but why the designer
seem can't access the removed properties ?

So, what is the different between overriding each property
BrowseableAttribute and this way?

I am asking this question because i have tried to remove some properties;
such as size and location. Well, these properties don't show in the
properties as i wish but the object in designer look weird. I can't resize
the object or change the location in the designer, but i can do set the size
or location in my code. Will it be the same effect if i override "Size" and
"Location" BrowsableAttribute not to remove these properties?

Hope your understand my point.

:

Oops, sorry thats my fault. working late on a saturday night, fresh batch of
Beaujolais Nouveau, you know how that is...

The interface is ICustomTypeDescriptor and it works like this;

The following application, after my signature, removes a bunch of inherited
properties from it's list. Note how ICustomTypeDescriptor must be fully
implemented, as you would expect. The two property grids essentially show a
Control object, the one on the right has had its properties editied
according to an array of property names held in the class.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace RemoveProps
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid1;
private System.Windows.Forms.PropertyGrid propertyGrid2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

this.propertyGrid1.SelectedObject=new Control();

this.propertyGrid2.SelectedObject=new FilteredControl();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.propertyGrid2 = new System.Windows.Forms.PropertyGrid();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.Size = new System.Drawing.Size(168, 264);
this.propertyGrid1.TabIndex = 0;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// propertyGrid2
//
this.propertyGrid2.CommandsVisibleIfAvailable = true;
this.propertyGrid2.LargeButtons = false;
this.propertyGrid2.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid2.Location = new System.Drawing.Point(224, 0);
this.propertyGrid2.Name = "propertyGrid2";
this.propertyGrid2.Size = new System.Drawing.Size(168, 264);
this.propertyGrid2.TabIndex = 0;
this.propertyGrid2.Text = "propertyGrid1";
this.propertyGrid2.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid2.ViewForeColor =
System.Drawing.SystemColors.WindowText;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 266);
this.Controls.Add(this.propertyGrid1);
this.Controls.Add(this.propertyGrid2);
this.Name = "Form1";
this.Text = "Form1";
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_SizeChanged(object sender, System.EventArgs e)
{
this.propertyGrid1.Location=new Point(0,0);
this.propertyGrid1.Size=new
Size(this.ClientRectangle.Width/2,this.ClientRectangle.Height);
this.propertyGrid2.Location=new Point(this.ClientRectangle.Width/2,0);
this.propertyGrid2.Size=new
Size(this.ClientRectangle.Width/2,this.ClientRectangle.Height);
}
}


class FilteredControl : Control, ICustomTypeDescriptor
{

private string[] NamesToRemove={
"AccesibilityObject",
"AccessibleDescription",
"AccessibleName",
"AllowDrop",
"BackgroundImage",
"Capture",
"DisplayRectangle",
"Enabled",
"ForeColor",
"Region",
"Tag",
"Text",
"Visible"};


//Does the property filtering...
private PropertyDescriptorCollection
FilterProperties(PropertyDescriptorCollection pdc)
{
ArrayList toRemove=new ArrayList();
foreach(string s in NamesToRemove)
toRemove.Add(s);

PropertyDescriptorCollection adjustedProps=new
PropertyDescriptorCollection(new PropertyDescriptor[]{});
foreach(PropertyDescriptor pd in pdc)
if(!toRemove.Contains(pd.Name))
adjustedProps.Add(pd);

return adjustedProps;
}



#region ICustomTypeDescriptor Members

public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this,true);
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

EventDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}

public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}


public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,
attributes, true);
return FilterProperties(pdc);
}

PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
PropertyDescriptorCollection pdc=TypeDescriptor.GetProperties(this,
true);
return FilterProperties(pdc);
}

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}

public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}

public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}

public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}

#endregion

}

}

------------------------------------------------------------------




"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
...
A more permanent way of hiding or adding properties is to make the
object
implement the ICustomPropertyProvider interface. This method enables the
derived class itself to limit or augment the properties seen when
reflection
is used to look at an object. The mechanism is similar to that of the
designer but encapsulated in the class itself. Here you can get the list
of
properties provided by the base class an remove the ones you don't wish
to
see anymore.

Bob, I don't find an ICustomPropertyProvider interface. Is that a .NET 2.0
post-beta 1 feature? I do see TypeDescriptionProvider in 2.0.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top