Base Designer Classes
All designers implement the IDesigner interface, which defines the basic designer interface methods. The .NET Framework also provides a set of base designer classes that provide methods that can be useful to designers that support specific types of components or controls.
A designer class must implement the IDesigner interface, as shown in the following code example.
Public Interface IDesigner
Sub Dispose()
Sub Initialize(component As IComponent)
' Other methods.
...
ReadOnly Property Component() As IComponent
' Other properties.
...
End Interface
public interface IDesigner {
void Dispose();
void Initialize(IComponent component);
// Other methods.
...
IComponent Component {
get;
}
// Other properties.
...
}
The Initialize method of an IDesigner is called after the component for the designer has been sited and initialized, and the designer has been created. You can override the Initialize method to perform actions that should occur at component or designer initialization time. You cannot replace the constructor initialization of the component, but you can augment it or reset the properties that it initializes. The Component property of an IDesigner is set through this initialization method. You should always call base.Initialize(component) from your Initialize method if you override this method. You can access the component of an IDesigner from its Component property.
The Component property provides access to the component that the designer is associated with. This property is set when the designer object is first created and its Initialize method is called. The component has a site associated with it, and the designer can use this site to obtain services from the designer's host.
The DoDefaultAction method is called when a component or control is double-clicked.
The Verbs property can be overridden to return a DesignerVerbCollection that contains the objects necessary to extend the menu items of a shortcut menu for a component.
The Dispose method is invoked when the designer object needs to be destroyed. It is called whenever a component is removed from the design container.
The ComponentDesigner class implements the IDesigner and IDesignerFilter interfaces to offer additional methods that can be useful to some designers of components.
The base designer class for Windows Forms controls is ControlDesigner. This class derives from ComponentDesigner and provides additional methods useful for customizing the appearance and behavior of a Windows Forms control. For a sample implementation of a Windows Forms designer, see How to: Implement a Designer for a Control.
The DocumentDesigner class provides a base designer for extending the design mode behavior of, and providing a root-level design mode view for, a Control that supports nested controls and receives scroll messages. For more information, see How to: Create a Windows Forms Control That Takes Advantage of Design-Time Features.
Observação
You must add a reference to the design-time assembly, System.Design.dll. This assembly is not included in the .NET Framework 4 Client Profile. To add a reference to System.Design.dll, you must change the project's Target Framework to .NET Framework 4.
The base designer class for ASP.NET server controls is ControlDesigner. This class provides the base functionality for custom design-time HTML rendering.
The TypeConverter class provides a base class for converting a type to and from a text representation. For more information about type converters, see How to: Implement a Type Converter or Generalized Type Conversion.
The UITypeEditor class provides a base class that you can derive from and extend to implement a custom type editor for the design-time environment. For more information on implementing a UITypeEditor, see User Interface Type Editors Overview.
The Behavior class can be extended to develop any type of user interface behavior, including selection, drag, and resize behaviors. Painting and hit-testing are performed by the Glyph class.
For more information, see Behavior Service Overview.
Observação
You must add a reference to the design-time assembly, System.Design.dll. This assembly is not included in the .NET Framework 4 Client Profile. To add a reference to System.Design.dll, you must change the project's Target Framework to .NET Framework 4.
The BasicDesignerLoader class provides an implementation of the IDesignerLoaderService interface. A BasicDesignerLoader is a complete implementation of a designer loader without anything relating to a persistence format.
CodeDomDesignerLoader is an abstract class that provides a full designer loader based on the Code Document Object Model (CodeDOM).
The CodeDomSerializerBase class provides a base class for CodeDomSerializer classes. The CodeDomSerializerBase class is used as a shared base between the CodeDomSerializer and TypeCodeDomSerializer classes.
The DesignerSerializationManager class provides an implementation of the IDesignerSerializationManager interface.
For more information, see Designer Serialization Overview.
The DesignerActionList class provides the base class for types that define a list of items used to create a smart tag panel. For more information, see How to: Attach Smart Tags to a Windows Forms Component.
Observação
You must add a reference to the design-time assembly, System.Design.dll. This assembly is not included in the .NET Framework 4 Client Profile. To add a reference to System.Design.dll, you must change the project's Target Framework to .NET Framework 4.
The LayoutEngine class provides the base class for implementing layout engines. The TableLayoutPanel and FlowLayoutPanel controls use the LayoutEngine class to provide layout behavior. For more information, see How to: Implement a Custom Layout Engine.
The Windows Software Development Kit (SDK) provides a set of designers to support specific types of components. These designers are named after the components they design, with the word Designer suffixed. For example, the designer for the System.Windows.Forms.Control class is System.Windows.Forms.Design.ControlDesigner.
Most types of design-time services can be requested through a GetService method by passing the type of the service to request. There is a GetService method on Component and ComponentDesigner. There is also a GetService method on IServiceProvider, which is implemented by the ISite returned by the Site property of an IComponent sited in design mode.
The following code shows how to obtain an IDesignerHost service interface and an IMenuCommandService using a GetService method.
For more information, see How to: Access Design-Time Services.
The following code demonstrates how to obtain a service from a design mode service provider interface.
' Obtain an IDesignerHost service from the design-time environment.
Dim host As IDesignerHost = CType(Me.Component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)
' Obtain an IMenuCommandService service.
Dim mcs As IMenuCommandService = CType(Me.Component.Site.GetService(GetType(IMenuCommandService)), IMenuCommandService)
// Obtain an IDesignerHost service from the design-time environment.
IDesignerHost host = (IDesignerHost)this.Component.Site.GetService(typeof(IDesignerHost));
// Obtain an IMenuCommandService service.
IMenuCommandService mcs =
(IMenuCommandService)this.Component.Site.GetService(typeof(IMenuCommandService));
A designer can obtain access to the components within a design mode document by accessing the Components collection of the Container property of an IDesignerHost service interface. The following example code shows how to access the components in the current design-mode project.
' Obtains an IDesignerHost service from the design-time environment.
Dim host As IDesignerHost = CType(Me.Component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)
' Gets the components container for the current design-time project.
Dim container As IContainer = host.Container
' The host.Container IContainer contains each IComponent in the project.
// Obtains an IDesignerHost service from the design-time environment.
IDesignerHost host = (IDesignerHost)this.Component.Site.GetService(typeof(IDesignerHost));
// Gets the components container for the current design-time project.
IContainer container = host.Container;
// The host.Container IContainer contains each IComponent in the project.
Once you have access to the components collection, you can use TypeDescriptor methods and PropertyDescriptor objects to identify types and set the values of properties of components. You can also create components using the CreateComponent method of the IDesignerHost interface.
When you derive from a component that has an associated designer, the designer of the base class is associated with the derived component by default. You can associate a different designer with your component by applying a DesignerAttribute attribute that specifies the type of designer to associate with the component. It is common for a derived component to have a designer that extends the base designer.
For more information, see How to: Implement a Designer for a Control.
Define a class that derives from the base designer class.
Associate the new designer class with your component by applying a DesignerAttribute.
The following code example defines a designer that extends System.Web.UI.Design.WebControls.LabelDesigner and associates it with a custom label that extends System.Web.UI.WebControls.Label.
Namespace MyControls.Design
Public Class MyLabelDesigner
Inherits System.Web.UI.Design.WebControls.LabelDesigner
End Class
End Namespace
Namespace MyControls
<Designer(GetType(MyControls.Design.MyLabelDesigner))> _
Public Class MyLabel
Inherits System.Web.UI.WebControls.Label
End Class
End Namespace
namespace MyControls.Design {
public class MyLabelDesigner : System.Web.UI.Design.WebControls.LabelDesigner {}
}
namespace MyControls {
[Designer(typeof(MyControls.Design.MyLabelDesigner))]
public class MyLabel : System.Web.UI.WebControls.Label {}
}
Observação
If you are defining a designer for a class that is sealed, or if you do not want other classes to use or inherit from your designer, you can make your designer class internal to your assembly. The designer host will still be able to create an instance of your designer, but it will not contribute to the public object model.
How to: Access Design-Time Services
How to: Access Design-Time Support in Windows Forms