Advertisement
Monday, September 06, 2010

.net Tips and Tricks

New .net Books!
 
LATEST NEWS

04.11.2005
BlueVision becomes a Microsoft Certified Partner!

03.30.2005

03.01.2005
Two development books get an update for Visual Studio .NET 2003

01.05.2005
BlueVision launches the Community Technology Preview release of CopAlert™, www.copalert.com

12.20.2002
BlueVision in MSDN Magazine
Tips and Tricks

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

Terms Of Use © 2000 - 2010 BLUEVISION LLC. ALL RIGHTS RESERVED. Privacy Policy