Click Here!
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


Here’s how we save those integers and start the drag operation with the Drag method:

Private Sub Textboxes_MouseDown(Index As Integer, Button As Integer, Shift _
    As Integer, X As Single, Y As Single)
    intXOffset = X
    intYOffset = Y
    Textboxes(Index).Drag vbBeginDrag
End Sub

Now the user is dragging the control—and we’ll see how to let the user drop it in the next topic.

Drag/Drop: Dropping Controls

When the user drops a control on a form, you get a DragDrop event:

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)

End Sub

Here, the control that’s been dropped is passed in the Source argument, and the position of the mouse is passed as (X, Y). You can use the control’s Move method to move the control to the new position.

Let’s see an example. In the previous topic, we let users drag a text box in a form. When they drop it, we’ll get a DragDrop event in the form and can move the text box to the new location—after taking into account the original mouse position in the control with the x and y offsets, intXOffset and intYOffset:

Dim intXOffset, intYOffset As Integer

Private Sub Form_Load()
    For Each objText In Textboxes
        objText.Text = "Drag me"
    Next
End Sub

Private Sub Textboxes_MouseDown(Index As Integer, Button As Integer, Shift _
    As Integer, X As Single, Y As Single)
    intXOffset = X
    intYOffset = Y
    Textboxes(Index).Drag vbBeginDrag
End Sub

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    Source.Move X - intXOffset, Y - intYOffset
End Sub

And that’s all it takes—now you can drag and drop controls in a form, as shown in Figure 28.3.


Figure 28.3  Dragging and dropping controls in a form.

However, there’s a problem here. When you move a text box to an entirely new location in the form, the drag/drop operation goes smoothly. However, if you drag the text box just a short distance, that text box jumps back to its original position when you release it—what’s going on?

Here’s what’s happening: if you drag the text box just a short distance and drop it, Visual Basic thinks you’re dropping it on itself, and instead of triggering a form DragDrop event, it triggers a DragDrop event for the text box itself. To complete our drag/drop example, we’ll take care of this “self-drop” problem in the next topic.

The code for this example, dragcontrols.frm version 1, appears in Listing 28.1. (Version 2, which is located in the dragcontrols folder on this book’s accompanying CD-ROM, will take care of the “self-drop” problem.)

Listing 28.1 dragcontrols.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 Textboxes
      Height          =   495
      Index           =   5
      Left            =   1920
      TabIndex        =   5
      Top             =   1920
      Width           =   1215
   End
   Begin VB.TextBox Textboxes
      Height          =   495
      Index           =   4
      Left            =   1920
      TabIndex        =   4
      Top             =   1200
      Width           =   1215
   End
   Begin VB.TextBox Textboxes
      Height          =   495
      Index           =   3
      Left            =   1920
      TabIndex        =   3
      Top             =   240
      Width           =   1215
   End
   Begin VB.TextBox Textboxes
      Height          =   495
      Index           =   2
      Left            =   360
      TabIndex        =   2
      Top             =   1920
      Width           =   1215
   End
   Begin VB.TextBox Textboxes
      Height          =   495
      Index           =   1
      Left            =   360
      TabIndex        =   1
      Top             =   1080
      Width           =   1215
   End
   Begin VB.TextBox Textboxes
      Height          =   495
      Index           =   0
      Left            =   360
      TabIndex        =   0
      Top             =   240
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim intXOffset, intYOffset As Integer

Private Sub Form_Load()
    For Each objText In Textboxes
        objText.Text = "Drag me"
    Next
End Sub

Private Sub Textboxes_MouseDown(Index As Integer, Button As Integer,_
    Shift As Integer, X As Single, Y As Single)
    intXOffset = X
    intYOffset = Y
    Textboxes(Index).Drag vbBeginDrag
End Sub

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    Source.Move X - intXOffset, Y - intYOffset
End Sub

Handling “Self-Drops” When Dragging And Dropping

In the previous two topics, we handled drag/drop operations for controls in a form, but there was a problem. If the user doesn’t move a control very far, we get a DragDrop event for the control itself, because Visual Basic acts as though the user is dropping the control on itself, not on the form. We can handle this case by adding a DragDrop event handler to the control itself.

Let’s see how this works in an example. We’ll add a DragDrop event handler for the text boxes in the previous example, the dragcontrols project:

Dim intXOffset, intYOffset As Integer
...
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    Source.Move X - intXOffset, Y - intYOffset
End Sub

Private Sub TextBoxes_DragDrop(Index As Integer, Source As Control, X As_
    Single, Y As Single)
...
End Sub

Now when the user drags a control a little way and drops it on top of itself, we can move the control to its new position. Note that we are passed mouse coordinates local to the control in the DragDrop event and have to translate them to form-based coordinates to use the Move method:

Dim intXOffset, intYOffset As Integer
...
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    Source.Move X - intXOffset, Y - intYOffset
End Sub

Private Sub TextBoxes_DragDrop(Index As Integer, Source As Control, X As_
    Single, Y As Single)
    Source.Move X + Textboxes(Index).Left - intXOffset, Y + _
        Textboxes(Index).Top - intYOffset
End Sub


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.