HOME   DOWNLOADS   ORDER ONLINE   CONTACT US



 OVERVIEW
 
 DOWNLOAD
 
 ORDER ONLINE
 
 KNOWLEDGE
 BASE

How to drag and drop items between two lists

"Drag and drop" is a standard Windows feature which most programs support in some form or other. Dragging items from one list into another list is not particularly an SBList innovation - the code shown below will work equally well with a standard list box.

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).

Copy this bitmap (somehow!) 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
Dim OrigIndex As Integer
These variables will be used to keep track of where the drag began and which list item we are dragging.

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
OrigIndex = SBList1(Index).ListIndex
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
  ' Drag this list item...
      SBList1(Index).ListIndex = OrigIndex
      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 set the list index to which ever item the MouseDown event was fired on (the mouse may have strayed onto the item above or below the original choice) and commence dragging.

5. Insert the following code in your SBList1_DragDrop event:

If Source.hWnd <> SBList1(Index).hWnd Then
  SBList1(Index).AddItem Source.List(OrigIndex)
  Source.RemoveItem OrigIndex
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 copy the item across and delete it from the source list.

This technique only works for dragging single list items. It's more complicated to drag multiple list items (like you can do in Explorer) but it is possible. This can only be done with an SBList, though. The general principle behind the code above can also be enhanced to allow the user to move list items around within the same list.

Topic devised by Andy Groom.

 
Copyright © 2000 Global Components. All rights reserved.