ComponentOne List 8.0 for ActiveX
Drag-and-Drop Behavior

Typically, implementing a drag-and-drop interface using a list is a painstaking task, since you normally want to drag data from a particular cell or row to another. Visual Basic's drag-and-drop facilities work with entire controls, but do not provide features for detecting which element of a control is involved.

True DBList has a special event, DragCell, designed to simplify the initiation of a drag operation. DragCell is called whenever the user attempts to drag data from a cell to another location; your code can respond accordingly. DragCell informs you of the split index, row bookmark, and column index of the cell being dragged. Typically, you will save the information so that it is available when the drag-and-drop operation terminates.

For example, assume that you want to be able to drag a row of data elsewhere. The following code in the DragCell event handler starts the drag-and-drop operation:

Example Title
Copy Code
Private Sub TDBList1_DragCell(ByVal SplitIndex As Integer, _

        RowBookmark As Variant, ByVal ColIndex As Integer)

 

' Set the current cell to the one being dragged

TDBList1.Col = ColIndex

TDBList1.Bookmark = RowBookmark

 

' Set up drag operation, such as creating visual effects by

' highlighting the cell or row being dragged.

 

' Use VB manual drag support (put TDBList1 into drag mode)

TDBList1.Drag vbBeginDrag

End Sub

Note that the Col and Bookmark properties of the list are set to reflect the cell that was dragged. Once this event is completed, Visual Basic takes over the drag operation (see the Visual Basic documentation for the Drag method). Place code in the DragDrop event of the destination to perform the actions related to the drop.

If the destination of a drag operation is another True DBList control, and you want to drop data into a row or cell, you need to consider the following:

Often, the difficulty with implementing such operations on lists is that, given the mouse location, it is difficult to find out which cell, row, or column you are about to drop into. True DBList solves this problem by providing the SplitContaining, ColContaining, and RowContaining methods, which translate mouse coordinates into a list location. You can also use the PointAt method to detect whether the drop position is over a data cell or a static element such as a caption bar.

Suppose that you want to provide feedback to the user about which cell is under the mouse pointer. The easiest way to do this is in the DragOver event, which fires as the mouse moves over the destination list. Here's how you would set the current cell pointer so that it tracks the dragging object:

Example Title
Copy Code
Private Sub TDBList2_DragOver(Source As Control, _

        X As Single, Y As Single, State As Integer)

' Set current cell to "track" the dragging object

Dim overCol As Integer

Dim overRow As Long

overCol = TDBList2.ColContaining(X)

overRow = TDBList2.RowContaining(Y)

If overCol >= 0 Then TDBList2.Col = overCol

If overRow >= 0 Then TDBList2.Row = overRow

End Sub

When the drop occurs (detected in the DragDrop event), you can move the appropriate data into the destination list, or perform whatever action you want the drop to trigger. For example, you can copy the contents of the dragged cell (which was made current in the DragCell example presented earlier) to the current cell in the destination list:

Example Title
Copy Code
Private Sub TDBList2_DragDrop(Source As Control, _

        X As Single, Y As Single)

TDBList2.Columns(TDBList2.Col).Value = _

    TDBList1.Columns(TDBList1.Col).Value

End Sub

You should also perform some clean-up when the drag-and-drop operation fails or the user completes the drop outside the boundaries of the destination control.

True DBList also supports OLE drag-and-drop.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback