Click here for Thousands of Animations
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


To let users drop OLE data, you use the OLEDragDrop event (this event occurs only if the object’s OLEDropMode property is set to Manual = 1):

Sub object_OLEDragDrop( data As DataObject,   effect As Long, _
   button As Integer,   shift As Integer,   x As Single, y As Single)

Here are the parameters passed to this event handler:

  data—A DataObject object containing data in formats that the source will provide.
  effect—A Long integer set by the target component identifying the action that has been performed, if any. This allows the source to take appropriate action if the component was moved. See the next list for the possible settings.
  button—An integer that gives the mouse button state. If the left mouse button is down, button will be 1; if the right button is down, button will be 2; and if the middle button is down, button will be 4. These values add if more than one button is down.
  shift—An integer that gives the state of the Shift, Ctrl, and Alt keys when they are depressed. If the Shift key is pressed, shift will be 1; for the Ctrl key, shift will be 2; and for the Alt key, shift will be 4. These values add if more than one key is down.
  x, y—The current location of the mouse pointer. The x and y values are in terms of the coordinate system set by the object (in other words, using the ScaleHeight, ScaleWidth, ScaleLeft, and ScaleTop properties).

Here are the possible values for the effect parameter:

  vbDropEffectNone—0; drop target cannot accept the data.
  vbDropEffectCopy—1; drop results in a copy of data from the source to the target. (Note that the original data is unaltered by the drag operation.)
  vbDropEffectMove—2; drop results in data being moved from drag source to drop source. (Note that the drag source should remove the data from itself after the move.)

To get the dragged data in the OLEDragDrop event, you use the DataObject’s GetData method, which returns the data stored in the format you’ve selected, if there is any:

DataObject.GetData ( format)

The format parameter here indicates the data format and may be set to one of these values:

  vbCFText—1; text (TXT) files
  vbCFBitmap—2; bitmap (BMP) files
  vbCFMetafile—3; Windows metafile (WMF) files
  vbCFEMetafile—14; enhanced metafile (EMF) files
  vbCFDIB—8; device-independent bitmap (DIB)
  vbCFPalette—9; color palette
  vbCFFiles—15; list of files
  vbCFRTF—-16639; Rich Text Format (RTF) files

Let’s see an example. In the previous topic, we’ve allowed the user to drag the text from a text box, Text1, to another text box, Text2, whose OLEDropMode property is set to Manual = 1. To place the dropped data into Text2, we just add a call to GetData to that text box’s OLEDragDrop event handler:

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As _
    Single, Y As Single)

    Text1.OLEDrag

End Sub

Private Sub Text1_OLECompleteDrag(Effect As Long)
    MsgBox "Returned OLE effect: " & Effect
End Sub

Private Sub Text1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)

    Data.SetData Text1.Text, vbCFText
    AllowedEffects = vbDropEffectMove

End Sub

Private Sub Text2_OLEDragDrop(Data As DataObject, Effect As Long, _
    Button As Integer, Shift As Integer, X As Single, Y As Single)
    Text2.Text = Data.GetData(vbCFText)
End Sub

And that’s it—now run the program, as shown in Figure 28.5. When you do, you can drag the text from the text box on the left, Text1, to the text box on the right, Text2, as shown in that figure. Our OLE drag/drop example is a success.


Figure 28.5  Dragging text data from one text box to another using OLE drag/drop.

The code for this example, oledrag.frm version 1, appears in Listing 28.2 (version 2, which is located on in the oledrag folder on this book’s accompanying CD-ROM, will report back to Text1 what happened when Text2 accepted the data).

Listing 28.2 oledrag.frm version 1

VERSION 6.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox Text2
      Height          =   495
      Left            =   2760
      OLEDropMode     =   1  'Manual
      TabIndex        =   1
      Top             =   1200
      Width           =   1215
   End
   Begin VB.TextBox Text1
      Height          =   495
      Left            =   480
      OLEDropMode     =   1  'Manual
      TabIndex        =   0
      Text            =   "OLE Drag!"
      Top             =   1200
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As _
    Single, Y As Single)
    Text1.OLEDrag

End Sub


Private Sub Text1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
    Data.SetData Text1.Text, vbCFText
    AllowedEffects = vbDropEffectMove
End Sub

Private Sub Text2_OLEDragDrop(Data As DataObject, Effect As Long, _
    Button As Integer, Shift As Integer, X As Single, Y As Single)
    Text2.Text = Data.GetData(vbCFText)
End Sub

OLE Drag/Drop: Reporting The Drag/Drop Outcome

When the user drops data into a target component during an OLE drag/drop operation, you can make sure the source component is informed of that fact with the OLECompleteDrag event:

Sub object_CompleteDrag([  effect As Long])

Here, the effect parameter can take these values:

  vbDropEffectNone—0; drop target cannot accept the data.
  vbDropEffectCopy—1; drop results in a copy of data from the source to the target. (Note that the original data is unaltered by the drag operation.)
  vbDropEffectMove—2; drop results in data being moved from drag source to drop source. (Note that the drag source should remove the data from itself after the move.)


Previous Table of Contents Next


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.