views:

82

answers:

3

Is this the way to hide properties in derived controls?

public class NewButton : Button

...

[Browsable ( false )]
public new ContentAlignment TextAlign { get; set; }

Also this hides the property in the Properties window in the designer but how can I also hide the property in code?

+1  A: 

Maybe what you want to do is derive from ContainerControl or UserControl, add a Button to that control and just expose those parts of the Button interface you want to keep.

Andrew Kennan
+2  A: 

You can use the [EditorBrowsable] attribute, as documented here.

[EditorBrowsable(EditorBrowsableState.Never)]
public bool HideMeInIntellisense
{
    // ...

From the documentation:

...the IntelliSense engine in Visual Studio uses this attribute to determine whether to show a property or method.

However, users can override this in VS settings. ReSharper also has a setting that controls whether this attribute is honoured in its IntelliSense.

Out of curiousity, why do you want to hide something from users? Just because a member is hidden in the way described above doesn't mean you couldn't use it in code and compile it successfully. It just inhibits the discoverability of the member.

Drew Noakes
By hiding I meant to completely "remove" it so you can't access it. It's because some of the properties of Button doesn't apply to my control.
Joan Venge
Have you considered subclassing a parent of the Button class and reimplementing what you need? I would consider asking a new question that digs into the reason why you need to change the button's behavior and looks for different ways to achieve that.
Drew Noakes
+1  A: 

From code, the closest you can do it to hide it, and perhaps make it a pain to call directly - note that even when hidden it is callable, and none of this will owrk past a cast:

// about the closest you can do, but not really an answer
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("just cast me to avoid all this hiding...", true)]
public new ContentAlignment TextAlign { get; set; }

Personally, I wouldn't bother. It isn't robust (just cast).

Marc Gravell
Thanks Marc. By cast you mean casting to the parent class in this case, Button?
Joan Venge