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

USING ACTIVEX CONTROLS 429

To test the enhanced ListBox control, place two buttons on the form, as shown in Figure 9.11. The Add New Item button prompts the user for a new item (a string) and adds it to the items ArrayList. Then it calls the Add method of the ListBox.Items collection to add the new item to the ListBox control. The reason you must add the new item to the ArrayList collection is that you can’t directly add items to the control. As you recall, the MeasureItem event adds one element of the ArrayList to the control at a time. Since both the MeasureItem and DrawItem methods pick up the item to be added from the ArrayList collection, you need not specify any argument to the ListBox1.Items.Add method. Listing 9.23 provides the code that adds a new item to the list.

Listing 9.23: Adding an Item to the List at Runtime

Private Sub Button2_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button2.Click Dim newItem As String

newItem = InputBox(“Enter item to add to the list”) items.Add(newItem)

ListBox1.Items.Add(“”) End Sub

The last feature in the test application is the reporting of the selected item when the user doubleclicks an item. I’ve included this code to demonstrate that, other than some custom drawing, the owner-drawn ListBox control carries with it all the functionality of the original ListBox control. It fires the same events, reports the same properties, and can be manipulated with the same methods.

When the user double-clicks the ListBox control, the code shown in Listing 9.24 is executed. This code retrieves SelectedIndex and SelectedItem properties and reports them to the application.

Listing 9.24: Retrieving the Selected Item from the Owner-Drawn ListBox Control

Private Sub ListBox1_DoubleClick(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles ListBox1.DoubleClick MsgBox(“Item at location “ & ListBox1.SelectedIndex & _

“ is “ & vbCrLf & ListBox1.SelectedItem)

End Sub

Using ActiveX Controls

Before ending this chapter, I would like to show you how to use ActiveX control with .NET. If you’re new to VB, ActiveX controls were the old Windows controls used with previous versions of VB. There are tons of ActiveX controls out there, and many of you are already using them in your projects. You can continue using them with your .NET projects as well. At this point, there’s a

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

430 Chapter 9 BUILDING CUSTOM WINDOWS CONTROLS

relatively small number of .NET controls, so for a while we will have to use the same controls we used in our VB6 projects.

One of my favorite ActiveX controls is the WebBrowser control. I happen to have this control because I’ve installed VB6. If you’re a VB6 programmer, you will have access to this control. If not, hopefully Microsoft will make these controls available with the release version of Visual Studio, or allow developers to download them. The WebBrowser control is nothing less than Internet Explorer in a control. This control can render any HTML document and Web page you can view with Internet Explorer, but it will do so in the context of a form. You can create forms that allow users to navigate the Web (or at least connect to your company’s Web site) from within your application. Figure 9.12 shows a Windows form with an instance of the WebBrowser control on it. The document displayed on the control is Visual Studio’s home page. You can navigate to any URL by typing an address in the TextBox control at the top of the form and clicking the Navigate button.

Figure 9.12

Navigating the Web with the WebBrowser control

By default, .NET doesn’t know how to handle ActiveX controls. To use an ActiveX control in a

.NET application, you must create a “wrapper” around the ActiveX control. The wrapper is a layer of code that allows .NET to communicate with the ActiveX control. .NET thinks it’s talking to a

.NET control, and the ActiveX control thinks its talking to a COM component. In effect, the wrapper is a translator that allows the two parties to communicate with one another, even though they don’t understand each other’s language. This is called a runtime callable wrapper (RCW), and it’s created for you by the CLR as soon as you add an ActiveX control to your Toolbox.

To add an ActiveX control, such as the WebBrowser control, to your Toolbox, open the Customize Toolbox dialog box (right-click the Toolbox and select Customize). In the COM Components tab, locate the ActiveX control you want to add, as shown in Figure 9.13.

Now design a form like the one shown in Figure 9.12 and enter the code from Listing 9.25 in the button’s Click event handler:

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

SUMMARY 431

Listing 9.25: Navigating to a URL with the WebBrowser Control

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

AxWebBrowser1.Navigate2(TextBox1.Text)

End Sub

Figure 9.13

Adding an ActiveX control to the Toolbox

That’s all it takes! You can also use this control to display local files to the users, such as help documents. Or allow them to connect to your site from within your application. I have found this control extremely useful in a large number of projects.

In Chapter 14, you’ll see how to use another ActiveX control, the Script control. The Script control allows you to execute short programs written in VBScript (a subset of Visual Basic). As you will see, it’s an extremely useful control that allows you to add scripting capabilities to your applications.

It is also possible to build your .NET controls so that they can be used in a COM environment, like Visual Studio 6. The process isn’t complicated, but you must first understand how ActiveX controls are registered in the system Registry. I won’t get into the details here, because this topic is of interest mostly to programmers who write .NET controls and want to expand their market by making their .NET controls compatible with earlier versions of Visual Studio. Please look up the documentation for specific information and step-by-step instructions on making your .NET control usable in the COM world.

Summary

One of the primary reasons for the phenomenal success of Visual Basic was that it could host custom controls. If you’ve been programming with VB for several years, you’ll remember the VBX controls. If you started using VB the last few years, you’ll remember the OCX controls. Now, it’s the

.NET controls. The controls keep getting better and so do the tools for creating custom controls.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

432 Chapter 9 BUILDING CUSTOM WINDOWS CONTROLS

The idea behind custom controls is that you can build a component with a visible and a programmatic interface and make it part of any development environment in the Windows world. Building custom controls isn’t just a task for the few companies that sell them. Even modest applications may call for custom controls. When you build a control, you’re actually encapsulating a lot of functionality in a black box, similar to a class. Unlike classes, however, controls have a visible interface too. Now that you know how to build your own classes and controls, you’re ready to distribute your code not only to end users, but developers as well. Even if you won’t make money by selling your components, there will always be a need for custom components in any programming environment. If you think that your code will be used repeatedly, either in multiple projects or by multiple programmers, consider packaging it as a custom class or control. The difference between the two is that controls expose the same functionality as classes but add a visible interface.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Chapter 10

Automating Microsoft Office

Applications

Another way to extend Visual Basic is to program the various objects exposed by the Office applications, or any other application that exposes an object model. Word, Excel, and Outlook expose rich object models that can be programmed, either from within the Office applications themselves, or in external applications, written in any .NET language. You can program any Office application with VBA or use VB.NET to program against the object model of each application. In this chapter, I’ll present the basic objects of the Office applications (Office 2000 and Office XP), because they are extremely popular and expose a whole lot of functionality.

I will limit the discussion to the three basic Office applications, Word, Excel, and Outlook, but once you understand how to manipulate their objects it shouldn’t be hard to look into the object models of PowerPoint, Project, and VISIO. Practically speaking, the most useful object model for VB programmers is that of Outlook, which allows you to read incoming messages or to create and send new ones from within applications written in VB.NET. Excel and Word also expose quite a bit of functionality, and you will see how you can tap into this functionality from within your VB applications.

An object model is a collection of classes that represents the objects, or entities, each application can handle, the properties that determine the characteristics of these entities, and the methods that act on them. The object model exposes all the functionality of its application, and you can program the exposed objects with any language. Word, for example, manipulates documents, which are made up of text, graphics, and tables (among other things). Word’s object model exposes the Document object, which represents a Word document. The Document object has a Range property, which represents a segment of the document (or all of it). The Range object, in turn, provides three collections—Paragraphs, Words, and Characters—which contain the corresponding entities of the document. All three collections provide the Font property, which lets you format the corresponding item by setting the font attributes. You can also apply a style to a paragraph or a word with the Style property.

As you will see, it is possible (and fairly straightforward) to start Word, create a new document, print it, and then close the application without ever displaying Word’s window. You can use this functionality to generate elaborate printouts from within your VB applications. You can

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com