|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Listing 26.3 olemultiple.frm version 1
VERSION 6.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3015
ClientLeft = 165
ClientTop = 735
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3015
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.OLE OLEControls
Height = 1215
Index = 1
Left = 840
SizeMode = 2 'AutoSize
TabIndex = 1
Top = 1680
Width = 3015
End
Begin VB.OLE OLEControls
Height = 1215
Index = 0
Left = 840
SizeMode = 2 'AutoSize
TabIndex = 0
Top = 240
Width = 3015
End
Begin VB.Menu File
Caption = "File"
Begin VB.Menu ActivateObject
Caption = "Activate object"
End
Begin VB.Menu CreateNewOLEControl
Caption = "Create new OLE control"
End
End
Begin VB.Menu Insert
Caption = "Insert"
Begin VB.Menu InsertObject
Caption = "Insert object"
End
Begin VB.Menu PasteSpecial
Caption = "Paste special"
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim intTotalOLEControls As Integer
Dim intXOffset, intYOffset As Integer
Private Sub ActivateObject_Click()
If TypeOf ActiveControl Is OLE Then
ActiveControl.DoVerb 0
End If
End Sub
Private Sub Form_Load()
intTotalOLEControls = 2
End Sub
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
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
Private Sub Form_Click()
Dim intLoopIndex As Integer
For intLoopIndex = 0 To intTotalOLEControls - 1
OLEControls(intLoopIndex).AppIsRunning = False
OLEControls(intLoopIndex).Visible = True
Next intLoopIndex
End Sub
Loading New OLE Controls At RuntimeThe olemultiple example weve developed in the previous two topics is designed to handle multiple OLE objects, but so far we only can handle two such objects because there are only two OLE controls in the program. OLE container programs, however, may be expected to handle more than two OLE objects, so well take a look at how to load additional OLE controls as required at runtime. Lets see an example. Well modify the olemultiple example now to let the user load in multiple OLE controls as required. To do that, add a new menu item, Create New OLE Control, to that programs File menu. When the user selects this item, we start by incrementing the total number of OLE controls, which weve stored in the variable intTotalOLEControls:
Private Sub CreateNewOLEControl_Click()
intTotalOLEControls = intTotalOLEControls + 1
...
Next, we load a new OLE control, adding it to our array of OLE controls, OLEControls:
Private Sub CreateNewOLEControl_Click()
intTotalOLEControls = intTotalOLEControls + 1
Load OLEControls(intTotalOLEControls - 1)
...
Now we position the new OLE control at upper left in the program, make it visible, and let the user insert an OLE object into it with the InsertObjDlg method:
Private Sub CreateNewOLEControl_Click()
intTotalOLEControls = intTotalOLEControls + 1
Load OLEControls(intTotalOLEControls - 1)
OLEControls(intTotalOLEControls - 1).Move 0, 0
OLEControls(intTotalOLEControls - 1).Visible = True
OLEControls(intTotalOLEControls - 1).InsertObjDlg
...
Finally, we can check if the object insertion operation was completed successfully and inform the user if not:
Private Sub CreateNewOLEControl_Click()
intTotalOLEControls = intTotalOLEControls + 1
Load OLEControls(intTotalOLEControls - 1)
OLEControls(intTotalOLEControls - 1).Move 0, 0
OLEControls(intTotalOLEControls - 1).Visible = True
OLEControls(intTotalOLEControls - 1).InsertObjDlg
If OLEControls(intTotalOLEControls - 1).OLEType = None Then
MsgBox "OLE operation failed."
End If
End Sub
And thats itnow we let the user add OLE controls as needed, using the Create New OLE Control menu item, as you can see in Figure 26.13. As you can also see in Figure 26.13, however, the placement of our new OLE object is less than optimal. Ideally, of course, the user can specify where in a container program the new OLE object should go, and well take a look at that in the next topic, where we let the user drag OLE controls in a form.
The code for this example, olemultiple.frm version 2, appears in Listing 26.4. (The final version, version 3which is located in the olemultiple folder on this books accompanying CD-ROMwill let the user drag and position OLE controls.)
|
|
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. |