0

I have a question please. Is it possible to hide some of the elements and categories of the base control (for a custom control). I want only the properties I defined to be shown. Thanks for your time.

None None
  • 195
  • 1
  • 3
  • 9

2 Answers2

1

Shadow the properties and add [Browsable(false)].

For example:

[Browsable(false)]
public new SomeType SomeProperty {
    get { return base.SomeProperty; }
    set { base.SomeProperty = value; }
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thanks a lot, Slaks. That did it. It is a shame that I cannot hide the properties from intellisense, in a decent manner. I have just found out about Tools -> Options -> Text editor -> C# -> IntelliSense -> Hide advanced members. Though, it is not practical in my opinion since it requires the control user to do it – None None Jan 19 '11 at 13:13
0

You can use the [Browsable(false)] custom attribute to prevent the property from appearing in the WinForms property editor:

[Browsable(false)]
public new PropertyType PropertyName
{
    get { return base.PropertyName; }
    set { base.PropertyName = value; }
}

However, this will make the property still work, it just won’t appear in the form designer. The compiler will happily accept it. If you want the property to actually stop working, throw exceptions:

[Browsable(false)]
public new PropertyType PropertyName
{
    get { throw new InvalidOperationException("This property cannot be used with this control."); }
    set { throw new InvalidOperationException("This property cannot be used with this control."); }
}

Of course, the compiler will still happily accept it, but it will throw at runtime. However, even so, the client programmer could still access the “original” property by casting to the base type, i.e. instead of

myControl.PropertyName

they can write

((BaseControlType) myControl).PropertyName

and it would still work. There is nothing you can do about this (short of deriving from a different base class).

Timwi
  • 65,159
  • 33
  • 165
  • 230
  • Actually, it will appear in IntelliSense. – SLaks Jan 18 '11 at 23:58
  • @SLaks: Interesting. Neither `Browsable` nor `EditorBrowsable` removes it from IntelliSense. Then how did WinForms manage to remove `Panel.KeyDown` from IntelliSense? – Timwi Jan 19 '11 at 01:16