Drag Method

See Also31LQ4GP              Example3DHUBZQ>Low

Begins, ends, or cancels dragging controls.

Syntax

[control.] Drag [action]

Remarks

The Drag method has the following parts:

Part               Description

 

control           Control to be dragged.  The Drag method can be used on any control except Line, Menu, Shape, or Timer.

action            Integer value from 0 to 2, inclusive, that indicates the action to perform.  If action is omitted, start dragging control.

 

Value            Meaning

 

0                   Cancel drag operation..

1                   Begin dragging control.

2                   End dragging, that is, drop control.

 

Use of the Drag method to control dragging and dropping is required only when the DragMode property of the control to drag is set to 0 (Manual).  However, you may still use Drag on a control whose DragMode property is 1 (Automatic).

If you want the mouse pointer to change while the control is being dragged, use either the DragIcon property or the MousePointer property to define your pointer.  Note that the MousePointer property is only used if no DragIcon is specified.


See Also

DragDrop Event1EBOJGL

DragIcon Property3G3M07O

DragMode PropertyS9LGP1

DragOver Event2TUGMAC

MousePointer Property1VSV6UM


Drag Method Example

The example uses the Drag method to drag the file name of a bitmap file to a picture box where the bitmap is displayed.  To try this example, paste all of the code into the Declarations section of a form that contains a drive list box, a directory list box, a file list box, a picture box, and a label.  Use the default names for the controls in all cases.  Size and position all controls so they can be easily seen and used.  The size and position of the label is unimportant as it will be changed at run time.  When the program begins you will be able to browse your file system and load any bitmaps.  Once you've located a bitmap that you want to display, click the file name of that bitmap and drag it to the picture control.

 

Sub Form_Load ()

   Picture1.AutoSize = -1                ' Turn on AutoSize.

   Label1.Visible = 0                    ' Make the label invisible.

   File1.Pattern = "*.BMP; *.ICO; *.WMF" ' Set file patterns.

End Sub

 

Sub Dir1_Change ()                       ' Any change in Dir1

   File1.Path = Dir1.Path                ' is reflected in File1.

End Sub

 

Sub Drive1_Change ()                     ' Any change in Drive1

   Dir1.Path = Drive1.Drive              ' is reflected in Dir1.

End Sub

 

Sub File1_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)

   Dim DY                                ' Declare variable.

   DY = TextHeight("A")                  ' Get height of one line.

   Label1.Move File1.Left, File1.Top + Y - DY /2, File1.Width, DY

   Label1.Drag                           ' Drag label outline.

End Sub

 

Sub Dir1_DragOver (Source As Control, X As Single, Y As Single, State As Integer)

   If State = 0 Then Source.MousePointer = 12  ' Change pointer to no drop.

   If State = 1 Then Source.MousePointer = 0   ' Use default mouse pointer.

End Sub

 

Sub Drive1_DragOver (Source As Control, X As Single, Y As Single, State As Integer)

   If State = 0 Then Source.MousePointer = 12  ' Change pointer to no drop.

   If State = 1 Then Source.MousePointer = 0   ' Use default mouse pointer.

End Sub

 

Sub Form_DragOver (Source As Control, X As Single, Y As Single, State As Integer)

   If State = 0 Then Source.MousePointer = 12  ' Change pointer to no drop.

   If State = 1 Then Source.MousePointer = 0   ' Use default mouse pointer.

End Sub

 

Sub File1_DragOver (Source As Control, X As Single, Y As Single, State As Integer)

   On Error Resume Next

   If State = 0 And Right$(File1.Filename,4) = ".ico" Then

      Label1.DragIcon = LoadPicture(File1.Path + "\" + File1.Filename)

   If Err Then MsgBox "The icon file could not be loaded."

   ElseIf State = 1 Then

      Label1.DragIcon = LoadPicture ()   ' Use no drag icon.

   End If

End Sub

 

Sub Picture1_DragDrop (Source As Control, X As Single, Y As Single)

   On Error Resume Next

   Picture1.Picture = LoadPicture(File1.Path + "\" + File1.Filename)

   If Err Then MsgBox "The picture file could not be loaded."

End Sub