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