Hiding elements and categories for custom controls

438 Views Asked by At

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.

2

There are 2 best solutions below

1
On

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

For example:

[Browsable(false)]
public new SomeType SomeProperty {
    get { return base.SomeProperty; }
    set { base.SomeProperty = value; }
}
2
On

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).