|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
You usually handle this problem by checking to see which control is the active controlthat is, the control with the focusin the program. In this way, the user clicks a control to give it the focus and uses the Insert Object menu item. Then you can use the Visual Basic form ActiveControl property to see which control to use. Lets see an example. Here, well create a new program, olemultiple, with two OLE controls, OLE1 and OLE2. Well develop this program over the next few topics to handle multiple OLE objects and even load new OLE controls in if the user wants to work with more than two objects at once. For now, add two OLE controls, OLE1 and OLE2 (setting their SizeMode properties to VbOLESizeAutoSize), to a new program, olemultiple. When the Insert Object dialog box comes up for each control, just click Cancel to make sure we dont insert any OLE objects before runtime. Add two menus as well: a File menu with the item Activate Object and an Insert menu with two items, Insert Object and Paste Special. When users select the Activate Object menu item, they want to activate the OLE object with the focus, and we can do that with the OLE controls DoVerb method. To determine which control to activate, we use the forms ActiveControl property. In fact, we can even make sure that the active control is really an OLE control by checking its type using the TypeOf keyword:
Private Sub ActivateObject_Click()
If TypeOf ActiveControl Is OLE Then
...
End Sub
If the control with the focus is an OLE control, we activate the object in that control, executing its primary verb:
Private Sub ActivateObject_Click()
If TypeOf ActiveControl Is OLE Then
ActiveControl.DoVerb 0
End If
End Sub
In the same way, we can insert a new OLE object into the OLE control with the focus using the InsertObjDlg method when the user clicks the Insert Object menu item:
Private Sub InsertObject_Click()
If TypeOf ActiveControl Is OLE Then
ActiveControl.InsertObjDlg
If ActiveControl.OLEType = None Then
MsgBox "OLE operation failed."
End If
End If
End Sub
And we can perform Paste Special operations on the OLE control with the focus when the user selects the Paste Special menu item:
Private Sub PasteSpecial_Click()
If TypeOf ActiveControl Is OLE Then
If ActiveControl.PasteOK Then
ActiveControl.PasteSpecialDlg
End If
If ActiveControl.OLEType = None Then
MsgBox "OLE operation failed."
End If
End If
End Sub
In fact, we can even deactivate our OLE objects when the user clicks the form outside any OLE control with the AppIsRunning property:
Private Sub Form_Click()
OLE1.AppIsRunning = False
OLE2.AppIsRunning = False
End Sub
Note that this last subroutine contains some awkward code: we need to address each OLE control by name (what if we had 50 OLE controls?), and addressing each control by name precludes the possibility of loading in new OLE controls at runtime (you need a control array for that). Well look at these two issues in the next topic. Using OLE Control Arrays To Handle Multiple OLE ObjectsWhen youve got a number of OLE objects to work with in a program, it helps to store them in an OLE control array because you can address those objects using an array index instead of by name, and that allows you to loop over them. Lets see an example. Here, we will modify the olemultiple example we started in the previous topic to use an array of OLE controls instead of individual OLE controls. As the program stands, we have to address each OLE control by name like this, where we deactivate any active objects when the user clicks the form:
Private Sub Form_Click()
OLE1.AppIsRunning = False
OLE2.AppIsRunning = False
End Sub
To install the OLE control array in the olemultiple example, delete the two OLE controls already there, OLE1 and OLE2. Add two OLE controls named OLEControls now, answering Yes when Visual Basic asks if you want to create a control array. Set these new controls SizeMode properties to AutoSize. Because well load new OLE controls into this program under user control in the next topic, we keep track of the total number of OLE controls in an integer named intTotalOLEControls: Dim intTotalOLEControls As Integer And we set intTotalOLEControls to 2 when we start the program:
Private Sub Form_Load()
intTotalOLEControls = 2
End Sub
Now when the user clicks the form to deactivate any running OLE objects, we can simply loop over the OLE controls in the OLEControls array:
Private Sub Form_Click()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To intTotalOLEControls - 1
OLEControls(intLoopIndex).AppIsRunning = False
Next intLoopIndex
End Sub
Now were able to insert two OLE objects into the olemultiple program, as shown in Figure 26.12. However, standard OLE container programs should be able to load new OLE controls on demand to handle additional objects as the user requires, and well see how to do that in the next topic.
The code for this example, olemultiple.frm version 1, appears in Listing 26.3 (version 2 will let the user load additional OLE controls as required). (Version 3, the final version of this example, is located in the olemultiple folder on this books accompanying CD-ROM.)
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |