How to: Access Design-Time Services
The following code example shows you how to gain access to the rich set of .NET Framework services so that you can integrate your components and controls into the design environment.
This code example demonstrates how to access services in your custom designer. A designer class called DemoControlDesigner is attached to a DemoControl class and performs the following operations:
-
Creates a Timer component using the CreateComponent method.
-
Uses the ShowCode method to start the Code Editor and display an event handler that is created when the Timer component is created.
-
Uses the DestroyComponent method to remove a Timer component from the design environment.
-
Uses GetExtenderProviders to enumerate all the extender providers and display them in a MessageBox.
-
Uses the GetReferences method to enumerate all the instances of DemoControl on the design surface.
-
Uses the GetPathOfAssembly method to display the path of the executing assembly.
-
Uses the GetComponentTypes method to find all the types that derive from ScrollableControl.
-
Uses the CategoryNames method to enumerate all the categories that appear in the Toolbox.
-
Sets a shadowed BackColor property on the designer, which is serialized by the design environment instead of the BackColor value of DemoControl.
-
Adds and removes properties using PreFilterProperties and PostFilterProperties methods.
The DemoControl class derives from UserControl class, but it requires no special logic to extend its design-time UI. The design-time UI is implemented by the DemoControlDesigner class.
Connecting to Services
The DemoControl class acquires references to various services in its InitializeServices method.
Service Events
Some services offer events to which your designer can attach. For example, the DemoControlDesigner class attaches event handlers to the ComponentChanged, ComponentAdded, and ComponentRemoved events.
Implementing the Dispose Method in the Designer
Be careful to detach your event handlers in your designer's Dispose method. This prevents unintended behavior when your designer goes out of scope.
Using Services
The DemoControl class uses several services in its smart tag panel. An internal class called DemoActionList is derived from the DesignerActionList class. This class has methods that enable you to create, delete, and enumerate various objects that are available in the design environment. It exposes these methods as smart tags. For example, the following code example uses the GetExtenderProviders method to enumerate all the extender providers present in the design environment.
When you make changes to the design-time aspects of a component, you need to rebuild the control project. In addition, if there is another Windows Forms project that is currently open and uses this component, you will probably need to refresh the project to see the changes. Typically, you need to close and reopen the design window containing the component.