Visions Of Afar
The Struggles of a College Student in the Gaming Industry

C# Property Grid Trick

July 5, 2009 13:44 by Garrett Hoofman

Say that you have class that's extending a Windows Form Control, and you want to use the Property Grid on that class. When you do that, all of the properties to the Windows Form Control are shown in the Property Grid, along with all of the properties you have for your class. This really clutters up the Property Grid with properties you don't care about.

So, the obvious thing would be to hide these properties using the System.ComponentModel namespace. How do we do that? Normally we would do something along the lines of  :

[Browsable(false)]

public object MyProperty {
get { return myProperty; } set { myProperty = value; } }

Doing this would hide the property from the Property Grid. However, we don't have direct access to the Windows Form Control class itself, so we need to find another way to hide the property.So we need to do this :

[Browsable(false)]

public new object WinFormControlProperty {
get { return base.WinFormControlProperty; }
set { base.WinFormControlProperty = value; } }

Now, all you have to do is replace the object type with the type of the of the property, and replace the WinFormControlProperty with the name of the property you're replacing.

If you're never going to use the property or you want to make sure that it's not going to be set, you can do this :

[Browsable(false)]
public new object WinFormControlProperty { get; set; }

Now you're Property Grid will be all cleaned up with only the items that you want shown.



Categories: C#
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading