Creating a New Component or Control

Starting from an existing WinForms project, you kick off the process of authoring a component by using the Add New Item dialog box (from the Project menu). Selecting Component Class in this dialog box adds the stub code file to your current project and launches the component designer. To start control development, you use the Add New User Control dialog box.


Note

Two different “types” of WinForms controls can be authored within Visual Studio: custom controls and user controls. Custom controls inherit directly from the System.Windows.Forms.Control class; they are typically code intensive because you, the developer, are responsible for writing all the code necessary to render the control’s visual portion. User controls (sometimes called composite controls) inherit from the System.Windows.Forms.UserControl class. User controls are advantageous because you can build them quickly by compositing other controls together that are already available in the Toolbox. These controls already have their user interface portion coded for you.


Both the control and the component designers work on the same principles as the Windows Forms Designer: The designers allow you to drag an object from the Toolbox onto the design surface.

Assume that you need a component that sends a signal across a serial port every x minutes. Because Visual Studio already provides a timer and a serial port component, which are accessible from the Toolbox, you can use the component designer to add these objects to your own custom component and then leverage and access their intrinsic properties and methods (essentially, using them as building blocks to get your desired functionality).

Figure 6.50 shows the component designer for this fictional custom component. Two objects have been added: a process component and a timer component.

Image

FIGURE 6.50 The component designer.

A similar scenario can be envisioned with a user control. You can take the example of a login “form,” consisting of two text boxes, two labels, and two buttons, and actually make that a control (one that can be easily included in the Toolbox and dropped onto a Windows form or web form).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset