Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

390 Chapter 8 BUILDING CUSTOM CLASSES

You can open the Matrix project on the CD, examine the code, and add more features to the Matrix class, starting with a method that inverts matrices (a complicated algorithm that I have not implemented in the sample project). You will notice that I’ve implemented the class in the form’s file. After adding all the functionality you need to the class, you can copy the class’s code into another project and create a class to use with any of your projects.

Summary

In this chapter, you learned the mechanics of building custom classes, and you were exposed to the main concepts of object-oriented programming. Inheritance and polymorphism are the two most powerful features introduced into the Visual Basic programming language, which bring it to the same level as the other two languages of Visual Studio.

The reason for using classes is code reuse. Classes are robust and not susceptible to changes. If another developer needs to extend your class, adding more members or overwriting existing members, they can do so by inheriting the functionality of your class. The existing applications will work with the old class, while newer applications can use the new one. You can also edit your class’s code without breaking any existing code. Just make sure you don’t change the interface of a class.

Now that you understand what classes are, how they work, and what they can do for you, we’re going to explore some useful classes that come with the .NET Framework. Of the numerous Framework classes, I’ve selected a few that most developers will be using on a daily basis and will discuss them at length in the following chapters. There are many more classes than I even list in this book. Once you familiarize yourself with the most basic ones, you will find it easier to discover the functionality of the other classes, or locate in the documentation the one that exposes the functionality you need.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Chapter 9

Building Custom

Windows Controls

Since version 5 of the language, VB has made it very simple to build custom controls, and it’s gotten even better with VB.NET. In addition to a host of controls that come with VB.NET and the capability to use ActiveX controls with .NET, you can also easily create your own custom

.NET controls.

The design of custom Windows controls has been one of the best implemented features of the language. Creating custom controls with VB.NET is even simpler, mostly because of the functionality added to the UserControl object.

Now, who should be developing custom Windows controls and why? If you come up with an interesting utility that can be used from within several applications, why not package it as a custom control and reuse it in your projects? You can also pass it to other developers and make sure that your application has a consistent look. For example, you might develop a custom control for designing reports or displaying lists of customers. Every form that uses this functionality should be implemented around this control. Users will learn to use the application faster, and the custom control will help you maintain a consistent look throughout the application.

In this chapter, you will learn how to design custom Windows controls for .NET. We’ll start by designing a new control that inherits an existing control and adds some extra functionality. Just as in the previous chapter we created a custom ArrayList that incorporated all the functionality of the original ArrayList and added some methods to it, we’ll do the same with the TextBox control. Then you’ll see how to build custom controls that combine multiple .NET controls. These controls are called compound controls, and they’re like regular forms with built-in functionality. The ComboBox control, for example, is a compound control made up of a TextBox (its edit area), a ListBox, and a Button that expands the list. Compound controls ride on the functionality of their constituent controls, and you can add specific functionality through custom properties and methods. Finally, you’ll learn how to build user-drawn controls. A user-drawn control is an empty surface, and you’re responsible for drawing the control’s interface (and updating it in response to external events).

We’ll also discuss a few interesting, out of the ordinary, topics, like how to take control of the drawing process of the MenuItem object and create menus with graphics. You’ll also learn how easy it is to build nonrectangular controls.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

392 Chapter 9 BUILDING CUSTOM WINDOWS CONTROLS

On Designing Windows Controls

Before I get to the details of how to build custom controls, I want to show you how they relate to other types of projects. I’m going to discuss briefly the similarities and differences among Windows controls, classes, and standard projects. This information will help you get the big picture and put together the pieces of the following sections.

A standard application consists of a main form and several (optional) auxiliary forms. The auxiliary forms support the main form, as they usually accept user data that are processed by the code in the main form. You can think of a custom control as a form and think of its Properties window as the auxiliary form. An application interacts with the user through its interface. The developer decides how the forms interact with the user, and the user has to follow these rules. Something similar happens with custom controls. The custom control provides a well-defined interface, which consists of properties and methods. This is the only way to manipulate the control. Just as users of your applications don’t have access to the source code and can’t modify the application, developers can’t see the control’s source code and must access a Windows control through the interface exposed by the control. When you develop a custom control, in turn, you can use any of the controls on the Toolbox. Once an instance of the control is on the form, you can manipulate it through its properties and methods and you never get to see its code.

In Chapter 8, you learned how to implement interfaces consisting of properties and methods and how to raise events from within a class. This is how you build the interface of a custom Windows control. You implement properties as Property procedures, and you implement methods as Public procedures. Whereas a class may provide a few properties and any number of methods, a control must provide a large number of properties. A developer who places your custom control on a form expects to see the properties that are common to all the controls that are visible at runtime (properties to set the control’s dimensions, its color, the text font, the Index and Tag properties, and so on). Fortunately, many of the standard properties are exposed automatically. The developer also expects to be able to program all the common events, such as the mouse and keyboard events, as well as some events that are unique to the custom control.

The design of a Windows control is similar to the design of a form. You place controls on a Form-like object, called UserControl, which is the control’s surface. It provides nearly all the methods of a standard form, and you can adjust its appearance with the drawing methods. In other words, you can use familiar programming techniques to draw a custom control, or you can use existing controls to build a custom control.

The forms of an application are the windows you see on the Desktop when the application is executed. When you design the application, you can rearrange the controls on a form and program how they react to user actions. Windows controls are also windows, only they can’t exist on their own and can’t be placed on the Desktop. They must be placed on forms.

The major difference between applications and custom controls is that custom controls can exist in two runtime modes. When the developer places a control on a form, the control is actually running. When you set a control’s property through the Properties window, something happens to the control; its appearance changes, or the control rejects the changes. This means that the code of the custom control is executing, even though the project on which the control is used is in design mode. When the developer starts the application, the custom control is already running. However, the control must be able to distinguish when the project is in design or execution mode and behave accordingly. Here’s the first property of the UserControl object you will be using quite frequently in your code: the DesignMode property. When the control is positioned on a form and used in the

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com