Beginning Visual C#supAND#174;/sup 2005

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:

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.

Figure 15-16

Категории