This documentation is archived and is not being maintained.

How to: Extend the Appearance and Behavior of Controls in Design Mode 

You can extend the design-time environment by authoring your own custom designer. Your custom designer can change the appearance and behavior of your control while the user is designing the control.

Example

The following code example demonstrates how to create a custom designer that extends the user interface (UI) for designing a custom control. A designer class called DemoControlDesigner is attached to a DemoControl class, enabling the following features:

  • Custom initialization of new DemoControl instances;

  • Visual representation of the control’s Margin and Padding properties;

  • Mouse and keyboard interaction to set the Anchor property;

  • Smart tag interface for setting the Anchor property.

No code example is currently available or this language may not be supported.

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.

The DemoControlDesigner class uses the Glyph, Behavior, and Adorner classes to extend the design-time experience for DemoControl. The visual aspects of the extended UI are implemented with the Glyph and Adorner classes. The mouse and keyboard interactions are implemented in the Behavior class.

You can extend the design-time appearance and behavior of your designer by overriding only ControlDesigner methods like OnPaintAdornments and OnMouseEnter, but the Glyph class provides a convenient way to factor appearance and behavior logic out of your designer.

Extending the Appearance

You extend the appearance of your custom design UI by implementing a Glyph class. The MarginAndPaddingGlyph class derives from the Glyph class. It paints two rectangles that represent the values of the control’s Margin and Padding properties. The MarginAndPaddingGlyph class handles the ComponentChanged event to update the display when the values of the control’s Margin or Padding properties change.

The following code example shows how to implement a MarginAndPaddingGlyph class which derives from Glyph.

No code example is currently available or this language may not be supported.

Extending the Behavior

You extend the behavior of your custom design UI by implementing a Behavior class. The Behavior class is subordinate to the Glyph class.

Caution noteCaution

The design environment does not permit Behavior objects that are not attached to a Glyph object.

You attach a Behavior object to a Glyph object in your Glyph type's constructor.

The following code example shows how to implement the AnchorGlyph constructor.

No code example is currently available or this language may not be supported.

The AnchorGlyph class paints selection handles that correspond to the value of the control's Anchor property. You override the GetHitTest method to return a Cursor object when the mouse pointer is over your glyph's hot spot. When the design environment receives a value from your GetHitTest method that is not null, it activates the Behavior object associated with your Glyph.

The following code example shows how to implement the AnchorGlyph class.

No code example is currently available or this language may not be supported.

The AnchorBehavior class implements the custom mouse interaction. You override Behavior class methods like OnMouseEnter to define your custom UI.

The following code example shows how to implement the AnchorBehavior class.

No code example is currently available or this language may not be supported.

Activating Your Design-Time User Interface

Enable your glyphs by creating an Adorner window and adding them to the Glyphs collection. Activate your custom design-time UI by adding your Adorner window to the Adorners collection of the BehaviorService. Perform these actions in your designer's Initialize method.

The following code example shows how to activate your design-time UI.

No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.

Compiling the Code

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.

See Also

Show: