|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to hide a object's inherited properties ?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 ? Look into the BrowsableAttribute class.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcomponentmodelbrowsableattributeclasstopic.asp -- Show quoteHide quoteTim Wilson ..Net Compact Framework MVP "mrVithan" <mrvithan at hotmail.com> wrote in message news:352CF090-E624-4024-A273-8466BE378937@microsoft.com... > 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 ? > "mrVithan" <mrvithan at hotmail.com> schrieb: You can try to override the property and mark it using the> 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 ? 'BrowsableAttribute' atttribute. If you want to hide the members in code too, take a look at the 'EditorBrowsableAttribute' too. 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" ? Show quoteHide quote "Herfried K. Wagner [MVP]" wrote: > "mrVithan" <mrvithan at hotmail.com> schrieb: > > 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. > > -- > Herfried K. Wagner [MVP] > <URL:http://dotnet.mvps.org/> > > > > If i do in the normal way by overriding the browserable for each inhertied AFAIK, this is the only way to do it.> properties one by one .... i think that would be a huge task to do and it is > a nightmare. -- Show quoteHide quoteTim Wilson ..Net Compact Framework MVP "mrVithan" <mrvithan at hotmail.com> wrote in message news:D42381F8-4DD3-40C3-BC20-44F1BAD1106F@microsoft.com... > 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" ? > > "Herfried K. Wagner [MVP]" wrote: > > > "mrVithan" <mrvithan at hotmail.com> schrieb: > > > 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. > > > > -- > > Herfried K. Wagner [MVP] > > <URL:http://dotnet.mvps.org/> > > > > > > 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. -- Show quoteHide quoteBob 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" <mrvithan at hotmail.com> wrote in message news:352CF090-E624-4024-A273-8466BE378937@microsoft.com... > 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 ? > Can anyone show me how to get all properties instance of inherited class ?
^^'' thank you Show quoteHide quote "Bob Powell [MVP]" wrote: > 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. > > > > > > "mrVithan" <mrvithan at hotmail.com> wrote in message > news:352CF090-E624-4024-A273-8466BE378937@microsoft.com... > > 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 ? > > > > > 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 ? Show quoteHide quote "Bob Powell [MVP]" wrote: > 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. > > > > > > "mrVithan" <mrvithan at hotmail.com> wrote in message > news:352CF090-E624-4024-A273-8466BE378937@microsoft.com... > > 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 ? > > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message Bob, I don't find an ICustomPropertyProvider interface. Is that a .NET 2.0 news:ek7rAy1zEHA.2572@tk2msftngp13.phx.gbl... .... > 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. post-beta 1 feature? I do see TypeDescriptionProvider in 2.0. John 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. -- Show quoteHide quoteBob 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 news:OS6zGZ7zEHA.3076@TK2MSFTNGP10.phx.gbl... > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > news:ek7rAy1zEHA.2572@tk2msftngp13.phx.gbl... > ... > > 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 > > 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. Show quoteHide quote "Bob Powell [MVP]" wrote: > 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 > news:OS6zGZ7zEHA.3076@TK2MSFTNGP10.phx.gbl... > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > > news:ek7rAy1zEHA.2572@tk2msftngp13.phx.gbl... > > ... > > > 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 > > > > > > > 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. -- Show quoteHide quoteBob 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" <mrvithan at hotmail.com> wrote in message System.Drawing.SystemColors.Window;news:05525833-418C-44E7-A2D5-F39A07743C82@microsoft.com... > 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 [MVP]" wrote: > > > 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 = > > this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.Window;> > 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 = Show quoteHide quote > > this.propertyGrid2.ViewForeColor = Point(this.ClientRectangle.Width/2,0);> > 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 Show quoteHide quote > > 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 > > news:OS6zGZ7zEHA.3076@TK2MSFTNGP10.phx.gbl... > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > > > news:ek7rAy1zEHA.2572@tk2msftngp13.phx.gbl... > > > ... > > > > 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 > > > > > > > > > > > > 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 .... ^^ Show quoteHide quote "Bob Powell [MVP]" wrote: > 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" <mrvithan at hotmail.com> wrote in message > news:05525833-418C-44E7-A2D5-F39A07743C82@microsoft.com... > > 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 [MVP]" wrote: > > > > > 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 > > > news:OS6zGZ7zEHA.3076@TK2MSFTNGP10.phx.gbl... > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > > > > news:ek7rAy1zEHA.2572@tk2msftngp13.phx.gbl... > > > > ... > > > > > 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 > > > > > > > > > > > > > > > > > > > > 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... Show quoteHide quote "mrVithan" wrote: > 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 [MVP]" wrote: > > > 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" <mrvithan at hotmail.com> wrote in message > > news:05525833-418C-44E7-A2D5-F39A07743C82@microsoft.com... > > > 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 [MVP]" wrote: > > > > > > > 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 > > > > news:OS6zGZ7zEHA.3076@TK2MSFTNGP10.phx.gbl... > > > > > "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message > > > > > news:ek7rAy1zEHA.2572@tk2msftngp13.phx.gbl... > > > > > ... > > > > > > 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 > > > > > > > > > > > > > > > > > > > > > > > > > > > >
Embedded .NET Control in IE - Policy issue not causing it to work???
Help with MS Accessibility Updating a windows forms control from a thread ANN: Bob Powell's Beginners Guide to GDI+ where is the Line Control ? Streaming with System.IO and System.Net Terminating a thread from the main thread Alternative to Windows.Forms Panel catch system shutdown event |
|||||||||||||||||||||||