HOME   DOWNLOADS   ORDER ONLINE   CONTACT US



 OVERVIEW
 
 DOWNLOAD
 
 ORDER ONLINE
 
 KNOWLEDGE
 BASE

How to drag and drop multiple list items

If you cast your mind back to Windows 3.1, you will probably remember File Manager. Dinosaur that it was, it did introduce one cool feature which was at that time new to Windows - multiple item drag and drop. You could select a range of files from the file list and then drag them into a different subdirectory or drive.

You can't achieve this with a standard list box because as soon as you highlight a range of list items and then try to begin a drag operation, all the selected items are cleared. SBList has an extra MultiSelect property to allow you to create a multiple item drag and drop list.

For this demonstration you will need to draw a form which has an SBList control array of two list boxes called SBList1(0) and SBList1(1). Set the MultiSelect property of both lists to setting "3 - Special".

Copy this bitmap into SDKPaint as a cursor and set the magenta pixels as being the screen colour. Remember to set the hotspot at the tip of the pointer arrow. Save the image as a cursor and then load it as the DragIcon image for your two SBLists.

1. Insert the following code into your General Declarations section:

Dim OrigX As Integer
Dim OrigY As Integer
These variables will be used to keep track of where the drag began.

2. Insert the following code into your Form_Load event:

For A% = 1 To 10
  SBList1(0).AddItem "List item " & A%
Next A%
This is just to give us some items to drag around.

3. Insert the following code into your SBList1_MouseDown event:

OrigX = X
OrigY = Y
We are storing the X and Y position of the mouse so that we can measure how far it has moved in the MouseMove event, and only start the drag operation when it's clear that the user is dragging rather than just clicking badly.

4. Insert the following code in your SBList1_MouseMove event:

If (Button And 1) = 1 Then
  If Abs(OrigX - X) > 45 Or Abs(OrigY - Y) > 45 Then
  ' Begin the Drag operation...
      SBList1(Index).Drag
  End If
End If
The user has the mouse button held down, and in the second line we are measuring how far it has moved since it was held down. The current tolerance is 45 twips in any direction (roughly 3 pixels). Once we are happy that the user is dragging we commence the drag operation.

5. Insert the following code in your SBList1_DragDrop event:

If Source.hWnd <> SBList1(Index).hWnd Then
  Source.AutoDraw = False
  SBList1(Index).AutoDraw = False
  ' Move the selected items across...
  Do While Source.SelCount
    If Source.Selected(A&) = True Then
      SBList(Index).AddItem Source.List(A&)
      Source.RemoveItem A&
    Else
      A& = A& + 1
    End If
  Loop
  Source.AutoDraw = True
  SBList1(Index).AutoDraw = True
Else
  Beep
End If
Source.Drag 0
This is the final step of the operation. If the item has been dropped into the same list as it was dragged from, we beep. Otherwise, we go through each item in the source list and copy across any selected items into the target list. When there are no more selected items we exit the loop. We turn off AutoDraw before we start moving items around so that (a) the operation is faster, and (b) the lists update simultaneously.

Topic devised by Andy Groom.

 
Copyright © 2000 Global Components. All rights reserved.