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

326 Chapter 7 MORE WINDOWS CONTROLS

If selStart = 0 Then

MsgBox(“No more matches”)

Exit Sub

End If

EditorForm.RTFBox.Select(selStart - 1, txtSearchWord.Text.Length)

EditorForm.RTFBox.ScrollToCaret()

End Sub

Notice that both event handlers call the ScrollToCaret method to force the selected text to become visible—should the Find method locate the desired string outside the visible segment of the text.

Summary

This chapter concludes the presentation of the Windows controls you’ll be using in building typical applications. There are a few more controls on the Toolbox that will be discussed in later chapters, and these are the rather advanced controls, like the TreeView and ListView controls. In addition, there are some rather trivial controls, which aren’t used as commonly as the basic controls. The trivial controls will not be discussed in this book. Instead, we’re going to move to some really exciting topics, like how to build custom classes and custom Windows controls.

Classes are at the core of VB.NET and extremely powerful. For the first time, VB classes support inheritance, which means you can extend existing classes, or existing Windows controls. You’ll learn how to build your own classes and inherit existing ones in the following chapter. Then, you’ll learn about building custom controls. Like classes, controls can also be inherited, and you’ll see how easy it is to extend the functionality of existing controls by adding new members.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Part II

Rolling Your

Own Objects

In this section:

Chapter 8: Building Custom Classes

Chapter 9: Building Custom Windows Controls

Chapter 10: Automating Microsoft Office Applications

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Chapter 8

Building Custom Classes

Classes are at the very heart of Visual Studio. Just about everything you do with VB.NET is a class, and you already know how to use classes. The .NET Framework itself is an enormous compendium of classes, and you can import any of them into your applications. You simply declare a variable of the specific class type, initialize it, and then use it in your code. As you have noticed, even a Form is a Class, and it includes the controls on the form and the code behind them. All the applications you’ve written so far are enclosed in a set of Class…End Class statements.

When you create a variable of any type, you’re creating an instance of a class. The variable lets you access the functionality of the class through its properties and methods. Even the base data types are implemented as classes (the System.Integer class, System.Double, and so on). An integer value, like 3, is actually an instance of the System.Integer class, and you can call the properties and methods of this class using its instance. Expressions like 3.MinimumValue and

#1/1/2000#.Today are odd, but valid.

In this chapter, you’ll learn how to build your own classes, which you can use in your projects or pass to other developers. Classes are used routinely in team development. If you’re working in a corporate environment, where different programmers code different parts of an application, you can’t afford to repeat work that someone else has done already. You should be able to get their code and use it in your application as is. That’s easier said than done, because you can guess what will happen as soon as a small group of programmers start sharing code. They’ll end up with dozens of different versions for each function, and every time they upgrade a function they will most likely break the applications that were working with the old version. Or, each time they revise a function, they must update all the projects using the old version of the function and test them. It just doesn’t work.

The major driving force behind object-oriented programming is code reuse. Classes allow you to write code that can be reused in multiple projects. You already know that classes don’t expose their source code. In other words, you can’t see the code in a class, and therefore you can’t affect any other projects that use the class. You also know that classes implement complicated operations and make these operations available to programmers through properties and methods. The Array class exposes a Sort method, which sorts its elements. This is not a simple operation, but fortunately you don’t have to know anything about sorting. Someone else has done it for you and made this functionality available to your applications. This is called encapsulation. Some functionality has

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

330 Chapter 8 BUILDING CUSTOM CLASSES

been built into the class (or encapsulated into the class), and you can access it from within your applications with a simple method call.

The 3,500 (or so) classes that come with the .NET Framework give you access to all the objects used by the operating system. All you have to do is use them in your application. You don’t have to see the code, and you don’t have to know anything about sorting to sort your arrays, just as you don’t need to know anything about encryption to encrypt a string with the System.Security.Cryptography class. In effect, you’re reusing code that Microsoft has already written. As you will see, it is also possible to extend these classes by adding custom members, and even override existing members. When you extend a class, you create a new class based on an existing one. Projects using the original class will keep seeing the original class, and they will work fine. New projects that see the derived class will also work.

In this chapter, you’ll learn how to create your own classes and share them with other programmers. This is one of the most improved areas of VB.NET, which is the first truly object-oriented version of Visual Basic. Most of the new functionality comes from the new techniques for implementing classes. Once you understand how classes are implemented and how to exploit features like inheritance, you’ll understand the topics discussed in earlier chapters a lot better. If you still have questions regarding the object-oriented features of the language, like the methods and properties exposed by the various data types, there’s a good chance that you’ll find the answers here. This chapter is not as much about techniques as it is about a good understanding of how classes work and why features like inheritance are really needed in a modern language—and, of course, why you shouldn’t go overboard with inheritance.

What Is a Class?

A class is a program that doesn’t run on its own; it must be used by another application. The way we invoke a class is by creating a variable of the same type as the class. Then, we exploit the func-

tionality exposed by the class by calling the members of the class through this variable. The methods and properties of the class, as well as its events, constitute the class’s interface. It’s not a visible interface, like the ones you’ve learned to design so far, and the class doesn’t interact directly with the user. To interact with the class, the application uses the class’s interface, just as users will be interacting with your application through its visual interface.

Until now, you have learned how to use classes. Now’s the time to understand what goes on behind the scenes when you interact with a class and its members. Behind each object, there’s a class. When you declare an array, you’re invoking the System.Array class, which contains all the code for manipulating arrays. Even when you declare a simple integer variable, you’re invoking a class, the System.Integer class. This class contains the code that implements the various properties (such as MinValue and MaxValue) and methods (such as ToString) of the Integer data type. The first time you use an object in your code, you’re instantiating the class that implements this object. The class is loaded into memory, initializes its variables, and is ready to execute. An instance of the class is ready for you to use.

Classes are very similar to Windows controls, only they don’t have a visible interface. Controls are instantiated when you place them on a form; classes are instantiated when you use a variable of the same type—and not when you declare the variable with the Dim statement. To use a control,

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com