Click here to Skip to main content
15,891,033 members
Articles / Web Development / ASP.NET
Article

Hiding Inherited Properties from the PropertyGrid

Rate me:
Please Sign up or sign in to vote.
4.62/5 (24 votes)
21 Aug 2005CPOL2 min read 128.5K   46   20
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.

C#
[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.

C#
[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.

C#
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)


Written By
Employed (other) Purplebricks
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.

Comments and Discussions

 
QuestionHow to use this code? Pin
Member 807586311-Nov-11 4:14
Member 807586311-Nov-11 4:14 
GeneralProperties Hidden From Editor Reappear After Closing/Opening Designer Window Pin
jkcarter16-Aug-07 4:51
jkcarter16-Aug-07 4:51 
GeneralRe: Properties Hidden From Editor Reappear After Closing/Opening Designer Window Pin
Shaun Wilde18-Aug-07 23:33
Shaun Wilde18-Aug-07 23:33 
GeneralRe: Properties Hidden From Editor Reappear After Closing/Opening Designer Window Pin
jkcarter19-Aug-07 6:24
jkcarter19-Aug-07 6:24 
QuestionWhere are the properties ??? Pin
Goudax22-Apr-07 22:44
Goudax22-Apr-07 22:44 
AnswerRe: Where are the properties ??? Pin
Shaun Wilde22-Apr-07 23:16
Shaun Wilde22-Apr-07 23:16 
GeneralRe: Where are the properties ??? Pin
Goudax22-Apr-07 23:21
Goudax22-Apr-07 23:21 
GeneralRe: Where are the properties ??? Pin
The Steve Mol8-Sep-20 7:38
The Steve Mol8-Sep-20 7:38 
Try Cleaning the project and then building it.
QuestionAssociating MyControlDesigner at runtime Pin
MatthewDBush19-Jun-06 20:54
MatthewDBush19-Jun-06 20:54 
AnswerRe: Associating MyControlDesigner at runtime Pin
Shaun Wilde20-Jun-06 8:02
Shaun Wilde20-Jun-06 8:02 
QuestionWhere is the sample code? Pin
Blkbam23-Apr-06 8:19
Blkbam23-Apr-06 8:19 
AnswerRe: Where is the sample code? Pin
Shaun Wilde23-Apr-06 8:40
Shaun Wilde23-Apr-06 8:40 
QuestionSet ControlDesigner in code? Pin
SureShot2K1-Dec-05 14:43
SureShot2K1-Dec-05 14:43 
AnswerRe: Set ControlDesigner in code? Pin
Shaun Wilde1-Dec-05 15:04
Shaun Wilde1-Dec-05 15:04 
GeneralNow all we need.. Pin
Ray Cassick22-Aug-05 4:22
Ray Cassick22-Aug-05 4:22 
GeneralRe: Now all we need.. Pin
Shaun Wilde22-Aug-05 5:10
Shaun Wilde22-Aug-05 5:10 
GeneralRe: Now all we need.. Pin
[Marc]23-Aug-05 7:58
[Marc]23-Aug-05 7:58 
GeneralRe: Now all we need.. Pin
Ray Cassick23-Aug-05 8:41
Ray Cassick23-Aug-05 8:41 
GeneralRe: Now all we need.. Pin
[Marc]23-Aug-05 12:33
[Marc]23-Aug-05 12:33 
GeneralRe: Now all we need.. Pin
The Steve Mol8-Sep-20 7:42
The Steve Mol8-Sep-20 7:42 

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

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