Make custom components invisible in the designer with DesignTimeVisibleAttribute
By default, all objects that derive from IComponent and that are placed on a
designer document are visible in the component tray. If the designer document
shown is of type System.Windows.Forms.Design.ComponentDocumentDesigner,
then the Component Tray fills the entire designer view. Consider the picture
below, where an instance of a MyComponent class named myComponent1 is
being designed:

And when the designer is one other than ComponentDocumentDesigner, the
ComponentTray displays below the view for the root designer, as shown:

In the picture above, there are actually two designers being utilized. First,
there is the root designer for the Form. Secondly, there is the designer for
the myComponent1 instance.
For the sake of argument, let's say that we did not want any instances of
MyComponent to be visible in the designer. In order to achieve this, we would
apply the System.ComponentModel.DesignTimeVisibleAttribute to
the class:
[DesignTimeVisible(false)]
public class MyComponent
Note: The Windows Forms TabPage component has this attribute applied so that the
addition of tab pages to the Tab control won't cause tons of tab pages to be
visible in the ComponentTray.
The ComponentTray class checks for this attribute in the implementation of its
CanDisplayComponent method, which is called by the AddComponent method. The
CanDisplayComponent method is protected and virtual, which means that it
can be overridden by custom ComponentTray classes.
According to the MSDN documentation, the DesignTimeVisibleAttribute is not
intended to be used directly from your code. But I disagree, as this attribute
will me used by many controls and designer developers to solve the same
problem. Otherwise, every developer would have to implement the ComponentTray
class and override its CanDisplayComponent method to return false, simply to
prevent components from showing up in the tray.
Back to Tips and Tricks