Creating Controls


There are times when the controls that ship with Visual Studio 2005 just don't meet your needs. The reasons for this can be many — the controls don't draw themselves in the way you want them to, the controls are restrictive in some way, or the control you need simply doesn't exist. Recognizing this, Microsoft has provided the means to create controls that meet your needs. Visual Studio provides a project type named Windows Control Library, which you use when you want to create a control yourself.

Two distinct kinds of home-made controls can be developed: user controls (or composite controls) and custom controls:

  • User or composite controls: These controls build on the functionality of existing controls to create a new control. Such controls are generally made to encapsulate functionality with the user interface of the control, or to enhance the interface of a control by combining several other controls into one unit.

  • Custom controls: These controls can be created when no control fits your needs, that is, you start from scratch. A custom control draws its entire user interface itself and no existing controls are used in the creation of the control. You will normally need to create a control like this when the user interface control you want to create is unlike that of any other available control.

In this chapter, you focus on user controls, because designing and drawing a custom control from scratch is beyond the scope of this book. Chapter 30 on GDI+ gives you the means to draw items by yourself, and you should then be able to move on to custom controls easily.

Note

ActiveX controls as used in Visual Studio 6 existed in a special kind of file with the extension ocx. These files were essentially COM DLLs. In .NET, a control exists in exactly the same way as any other assembly, and because of that the ocx extension has disappeared and controls exist in DLLs.

User controls inherit from the System.Windows.Forms.UserControl class. This base class provides the control you are creating with all the basic features a control in .NET should include — leaving you only the task of creating the control. Virtually anything can be created as a control, ranging from a label with a nifty design to full-blown grid controls. In Figure 15-16, the box at the bottom, UserControl1, represents a new control.

image from book
Figure 15-16



Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net