How to: Create and Configure Components in Design Mode
Your custom designer can programmatically create and configure components on the design surface.
The following code example demonstrates how to use designer services to create and initialize components in your custom designer. A designer class called DemoControlDesigner is attached to a DemoControl class and performs the following operations:
-
Creates and configures a Timer component for display on the design surface.
-
Removes a Timer component from the design surface.
-
Initializes an instance of the DemoControl class when it is created by the Toolbox.
You can create and configure a component by requesting an instance from the design environment and setting its properties. You can also configure a component when it is created from the Toolbox.
Creating and Configuring a Component Programmatically
The following code example calls the CreateComponent method to create a Timer component. The Timer component is initialized by setting its Interval and Enabled properties.
This example also uses the GetEventProperty method to attach an event handler.
Removing a Component Programmatically
The following code example calls the DestroyComponent method to remove a Timer component from the design environment.
Initializing a Component Created by the Toolbox
You can also configure components when they are created by the Toolbox. You implement a custom ToolboxItem and override the CreateComponentsCore method.
Attach your component to your ToolboxItem class by using ToolboxItemAttribute.
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.