Click here to Skip to main content
Click here to Skip to main content

Hiding Inherited Properties from the PropertyGrid

, 21 Aug 2005
Rate this:
Please Sign up or sign in to vote.
Two techniques for hiding properties, inherited from base controls, from the PropertyGrid.

Introduction

When creating a control you will normally start with the lowest common denominator base control that provides you with some base functionality. Sometimes however the control you start with may present properties to the PropertyGrid that you do not wish to expose in your new control in the Designer as they may no longer be relevant. Here we explain two ways that you can hide these properties which you may find useful.

Hiding properties from the PropertyGrid

To hide properties from the PropertyGrid is simple all you need to use is the BrowsableAttribute and set its value to false.

[Browsable(false)]
public int AnIntegerProperty
{
    get
    {
        ...
    }
    set
    {
        ...
    }
}

So, to hide an inherited property all you need to do is encapsulate that property and add the attribute.

[Browsable(false)]
public override string Text
{
    get
    {
        ...
    }
    set
    {
        ...
    }
}

Sometimes you find that you may need to add the virtual or new keyword depending on the circumstances. Now this is an easy approach, however all you are doing is adding needless code in order to get the PropertyGrid to display what you want it to display.

An alternative approach

When you create a control it is a common practice to associate a designer with the control and using a designer allows you to separate the design-time functionality of your control from the run-time. A designer has an override called PreFilterProperties that can be used to add extra attributes to the collection that has been extracted by the PropertyGrid.

using System.ComponentModel;

[ToolboxData("<{0}:MyControl runat="server"></{0}:MyControl>"),
Designer(typeof(MyControlDesigner))]
public class MyControl : ...
{
    ...
}

public class MyControlDesigner : ...
{
    ...

    protected override void PreFilterProperties(
                             IDictionary properties) 
    {
        base.PreFilterProperties (properties);
        string[] propertiesToHide = 
                     {"MyProperty", "ErrorMessage"};
        
        foreach(string propname in propertiesToHide)
        {
            prop = 
              (PropertyDescriptor)properties[propname];
            if(prop!=null)
            {
                AttributeCollection runtimeAttributes = 
                                           prop.Attributes;
                // make a copy of the original attributes 
                // but make room for one extra attribute
                Attribute[] attrs = 
                   new Attribute[runtimeAttributes.Count + 1];
                runtimeAttributes.CopyTo(attrs, 0);
                attrs[runtimeAttributes.Count] = 
                                new BrowsableAttribute(false);
                prop = 
                 TypeDescriptor.CreateProperty(this.GetType(), 
                             propname, prop.PropertyType,attrs);
                properties[propname] = prop;
            }            
        }
    }
}

By adding the names of the properties you wish to hide to the collection propertiesToHide, you now have a quick and easy way of hiding properties without writing lots of needless code. With a little extra effort you can add this code to a base designer class and add it to all your controls where required.

Known issues

It has been noted that some properties when manipulated in this way may cause issues when the control is being edited. If you have an issue then you can use the first solution in order to hide the property, an example is the Text property when using a control based on the BaseValidator. It is still being investigated if it is always the Text property or if some other issue is causing the misbehavior.

Comments

Please take the time to vote for this article and/or to comment about it on the boards below. All suggestions for improvements will be considered.

History

  • 22nd August, 2005 - v1
  • 25th August, 2005 - v1.1 - added known issues.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shaun Wilde
Software Developer (Senior) MYOB
Australia Australia
All articles are supplied as-is, as a howto on a particular task that worked for me in the past. None of the articles are supposed to be out-of-the-box freeware controls and nor should they be treated as such. Caveat emptor.
 
Now living and working in Australia, trying to be involved in the local .NET and Agile communities when I can.
 
I spend a good chunk of my spare time building OpenCover and maintaining PartCover both of which are Code Coverage utilities for .NET.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionHow to use this code? PinmemberMember 807586311-Nov-11 4:14 
GeneralProperties Hidden From Editor Reappear After Closing/Opening Designer Window Pinmemberjkcarter16-Aug-07 4:51 
GeneralRe: Properties Hidden From Editor Reappear After Closing/Opening Designer Window PinmemberShaun Wilde18-Aug-07 23:33 
GeneralRe: Properties Hidden From Editor Reappear After Closing/Opening Designer Window Pinmemberjkcarter19-Aug-07 6:24 
QuestionWhere are the properties ??? PinmemberGoudax22-Apr-07 22:44 
AnswerRe: Where are the properties ??? PinmemberShaun Wilde22-Apr-07 23:16 
GeneralRe: Where are the properties ??? PinmemberGoudax22-Apr-07 23:21 
QuestionAssociating MyControlDesigner at runtime PinmemberMatthewDBush19-Jun-06 20:54 
AnswerRe: Associating MyControlDesigner at runtime PinmemberShaun Wilde20-Jun-06 8:02 
QuestionWhere is the sample code? PinmemberBlkbam23-Apr-06 8:19 
AnswerRe: Where is the sample code? PinmemberShaun Wilde23-Apr-06 8:40 
QuestionSet ControlDesigner in code? PinmemberSureShot2K1-Dec-05 14:43 
AnswerRe: Set ControlDesigner in code? PinmemberShaun Wilde1-Dec-05 15:04 
GeneralNow all we need.. PinmemberRay Cassick22-Aug-05 4:22 
GeneralRe: Now all we need.. PinmemberShaun Wilde22-Aug-05 5:10 
GeneralRe: Now all we need.. Pinmember[Marc]23-Aug-05 7:58 
GeneralRe: Now all we need.. PinmemberRay Cassick23-Aug-05 8:41 
GeneralRe: Now all we need.. Pinmember[Marc]23-Aug-05 12:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 22 Aug 2005
Article Copyright 2005 by Shaun Wilde
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid