Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / C#
Tip/Trick

Hiding Inherited Properties and Tasks in Derived Classes

Rate me:
Please Sign up or sign in to vote.
4.82/5 (27 votes)
5 Dec 2015CPOL2 min read 64.8K   18   14
Hiding inherited properties and tasks in derived classes

Hiding Properties

When you create a custom control (or any class), you inherit a lot of properties from the base class, most of which are fine. However, particularly in the case of a control derived from UserControl, this brings a lot of properties which may not be relevant to the new control.

For example, if your control never scrolls, then the UserControl inherited property AutoScroll is confusing, and makes your control look unprofessional.

The solution is to hide them. However, actually hiding them took me quite a while to work out:

Create a new property with the same signature as the property you wish to hide, and give it a default getter and setter:

C#
/// <summary>
/// Unused property
/// </summary>
[Browsable(false),
EditorBrowsable(EditorBrowsableState.Never)]
public new bool AutoScroll { get; set; }

The Browsable attribute tells the designer that it should not appear in the Properties window.
The EditorBrowsable attribute tells Intellisense that it should not appear in the autocomplete list.

When you run this, you will find that the AutoScroll property still appears in Intellisense! Annoying, isn't it? Don't worry.
It will only appear in Intellisense within your solution.
If you add a reference to your control library within an different solution, Intellisense will not list the property. (You can however still access it if you type the name in fully. Irritating, but true.)

Hiding Tasks

In addition, you can hide the Task list for a derived control.

In the Visual Studio designer, when you add a component, it shows a "tasks" indicator for some. For example, the TextBox control has a Task which allows the designer to change the Multiline property without accessing the property page:

If you create a class derived from Textbox that doesn't support multiline (a password entry textbox for example), then even if you have "hidden" the Multiline property using the code above, it still appears in the Task.

You can hide it though, by decorating your class with this attribute:

C#
/// <summary>
/// Password Textbox: does not expose the entered string.
/// </summary>
/// <remarks>
/// The "Designer" attribute decorating this class is necessary.
/// This hides the default TextBox Task in the designer - which
/// consists of the MultiLine property, which we don't have!
/// </remarks>
[Designer(@"System.Windows.Forms.Design.ControlDesigner, System.Design," +
            " Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public class PasswordTextBox : TextBox
    {

This removes the Task indicator in the designer.

History

  • 3rd October, 2011: Original version
  • 6th December, 2015: Added Task hiding and headings
  • 6th December, 2015: Changed title to reflect Task removal

License

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


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
QuestionHiding Properties Pin
Milton N8-Dec-15 1:32
Milton N8-Dec-15 1:32 
AnswerRe: Hiding Properties Pin
OriginalGriff8-Dec-15 1:48
mveOriginalGriff8-Dec-15 1:48 
No, because it's part of the System.Web.UI.Design namespace, and doesn't exist to override for windows controls.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

Questionmaking it private? Pin
John Torjo8-Dec-15 0:48
professionalJohn Torjo8-Dec-15 0:48 
AnswerRe: making it private? Pin
OriginalGriff8-Dec-15 1:03
mveOriginalGriff8-Dec-15 1:03 
GeneralRe: making it private? Pin
John Torjo8-Dec-15 1:13
professionalJohn Torjo8-Dec-15 1:13 
AnswerRe: making it private? Pin
OriginalGriff8-Dec-15 1:48
mveOriginalGriff8-Dec-15 1:48 
QuestionBrilliant! Pin
maxpayne0026-Sep-14 10:12
maxpayne0026-Sep-14 10:12 
QuestionThis solution is so annoying... Pin
Sander Rossel21-Jan-13 4:54
professionalSander Rossel21-Jan-13 4:54 
GeneralMy vote of 5 Pin
Sushil Mate23-Oct-12 17:54
Sushil Mate23-Oct-12 17:54 
GeneralMy vote of 5 Pin
Jörgen Andersson23-Sep-12 10:00
professionalJörgen Andersson23-Sep-12 10:00 
GeneralReason for my vote of 5 nice one. Pin
Nikhil_S1-Mar-12 17:53
professionalNikhil_S1-Mar-12 17:53 
GeneralReason for my vote of 5 Thanks! Pin
beginner201127-Sep-11 22:41
beginner201127-Sep-11 22:41 
GeneralReason for my vote of 5 This is very cool. Thanks! Pin
[d3m0n]15-Mar-11 11:39
[d3m0n]15-Mar-11 11:39 
GeneralReason for my vote of 5 simple and effective Pin
Mauxel7-Jul-10 3:39
Mauxel7-Jul-10 3:39 

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.