Building Windows Forms controls and components with rich design-time features
Michael weinhardt and Chris sells
Design-time Architecture
Design mode is activated the moment a form is opened for editing. it can involve activities like resizing, setting the border style, changing the caption, and adding controls and components, which also enter design mode the moment they are dragged onto either the visual or nonvisual design surface. the nonvisual area, or component tray, hosts components. A component is a framework class that supports the design-time experience but doesn't draw its own UI in a container-specified region. components implement the system. componentmodel. icomponent interface, typically by deriving from the systemcomponent. model. component base class. controls, on the other hand, do draw themselves and are therefore shown in the visual area. all controls ultimately derive from the system. windows. forms. control class, which in turn derives from the component base class. in other words, a control is really a component with a UI. both act nearly identically at design time in terms of their integration with the design host. figure 1 shows a form in design mode acting as a design host for a number of components and controls.
While both components and controls ultimately derive from the component base class, that's not what distinguishes a component from any other class. instead, what makes the component class fit into the design-time architecture is its implementation of the icomponent interface. icomponent allows components to integrate with the design-time host, providing them with design-time services.
Design-time Architecture
Once the form is opened and edited, the design mode is activated. It can accept many behaviors, such as adjusting the size, setting the border style, and changing the title. It also includes adding control and component ), when they are dragged to a visible or non-visible area, they also enter the design mode. A non-visible area, or a component tray, is used to store components. A component is a framework class that supports experience during design, but it does not draw its own UI (User Interface) in the region specified by the container ). The component implements the system. componentmodel. icomponent interface. More typically, it inherits from the base class system. componentmodel. component. On the other hand, controls (DO) Draw themselves, so they can be displayed in the visible area. All controls ultimately inherit from the system. Windows. Forms. control class, while the latter inherits from the component base class. In other words, a widget is actually a component with a UI. During the design period, the combination of the two and the design host was almost the same. Figure 1 shows a form that acts as the design host for many components and controls in design mode.
Since both components and controls ultimately inherit from the component base class, components are no different from other classes. Instead makes the component class suitable for The Implementation of The icomponent interface in the design architecture. Icomponent allows components to be combined with the design-time host and provide them with design-time services.
Hosts and containers
In Visual Studio. net, the Windows Forms designer is responsible for providing design-time services during Windows Forms development, including a form's UI and code views. the responsibility of managing integration between design-time objects and the designer is handled by an internal implementation of system. componentmodel. design. idesignerhost. the designer host stores icomponent references to all design-time objects on the current form as well as the form itself, which is also a component. this collection of components is available from the idesignerhost interface through the property of type system. componentmodel. icontainer:
This implementation of icontainer allows the designer host to establish a relationship that helps it manage each of the components placed on the form.
Host and container
In Visual Studio. NET Medium, Windows The Form Designer is responsible Windows Provide design services during form development, including forms UI AndCodeView. The task for managing integration between objects and the designer at design time is composed of one System. componentmodel. Design. idesignerhost Interface. The designer host stores Icomponent Reference, including the window itself, because it is also a component. This component set can be Idesignerhost Interface Container Attribute access. The attribute type is System. componentmodel. icontainer :
Interface Icontainer: idisposable {
Componentcollection Components {Get;}
Void Add (icomponent component );
Void Add (icomponent component, String
Name );
Void Remove (icomponent component );
}
This implementation of the icontainer interface allows the designer host to establish a relationship to help it manage every component placed on the form.
Sites
At design time, contained components can access the designer host, as well as each other, through their container. this two-way relationship is shown in figure 2. you can also see that the fundamental relationship between the designer host and its components is established with an implementation of the system. componentmodel. isite interface:
During design, the included components and designer hosts can access each other through the containers between them. This bidirectional relationship is shown in figure 2. As you can see, the basic relationship between the designer host and its components is established by implementing a system. componentmodel. isite interface: Interface Isite: iserviceprovider {
Icomponent component {Get;}
Icontainer container {Get;}
Bool DesignMode {Get;}
String Name {Get;Set;}
}
Internally, a container stores an array of sites. when each component is added to the container, the designer host creates a new site, connecting the component to its design-time container and vice versa, By caching the isite interface in the icomponent. site property implementation.
Internally, a container stores a site array. Whenever a component is added to a container, the designer host creates a new site and caches the icomponent. the isite interface implemented in the site property, connecting components and their containers at design time, and vice versa. Interface Icomponent: idisposable {< br> isite site { Get ; set ;}
event eventhandler disposed;
}
The component base class implements icomponent and caches the site's interface in a property. it also provides a helper property to go directly to the component's container without having to go through the site first:
The component base class implements the icomponent interface and caches the site in the attribute. At the same time, it provides a Secondary attribute to directly access the component container, instead of using the site: Class Component: externalbyrefobject, icomponent, idisposable {
Public Icontainer container {Get;}
Public Isite site {Virtual Get;Virtual Set;}
}
The component base class provides a component with access to both the container and the site directly. A component can also access the Visual Studio. NET designer host by requesting the idesignerhost interface from the container:
The component base class provides a component that allows you to directly access containers and sites. You can also request the container's idesignerhost interface to access the designer host of Visual Studio. NET.Idesignerhost designerhost= This. ContainerAs
Idesignerhost;
In Visual Studio. net, the designer has its own implementation of the idesignerhost interface, but to fit into other designer hosts, it's best for a component to rely only on the interface and not any specific implementation.
In Visual Studio. in. net, the designer has its own implementation of the idesignerhost interface. However, to adapt to other designer hosts, it is recommended that the component only depends on the interface instead of its specific implementation.