Thursday, February 25, 2010

Hide a Base class property in Inherited class (Custom Control)

In case you are extending a control and you want your extended control not to display the Base control's (class) property in the designer view then you should be follwing this steps.

Example:
-----------
Suppose you are extending the TreeView Control and you want the TreeView Control's ShowCheckBoxes property to be hidden in the extended Control. Here are the steps you should follow to establish that.

1> Create a Class,i.e, TreeViewExtender, inherit it from TreeView Class.
2> Create a property with the same Name and ReturnType as it is in the Base Class.
3> Use the new operator whch is used to explicitly hide a member inherited from a base class. (To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.)
4> Use the BrowsableAttribute and set its value to false.

public class TreeViewExtender : TreeView
{
#region Properties

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

#endregion
}

Note: The [Browsable(false)] is for whether it shows up in the designer property grid or not but it would be accessed from the editor or code behind.
In order to stop the assignment to this property make it a read-only property by removing the setter from the Property in the Derived Class.

No comments: