Visual Basic Code of the Week (COTW)
Issue #1 - Premier Issue
All content and source code is Copyright (c) 1997 by C&D Programming Corp. None of the source can be reprinted in any manner without express written permission of C&D Programming Corp.
If you would like to view this page in HTML format, point your browser to


Welcome to the premier issue of Code of the Week for Visual Basic. You have found what promises to be the best source code resource for your Visual Basic development.

Requirements for this Issue

The source code in this issue is designed for Visual Basic 4.0 16-bit. Similiar code can be implemented in the 32-bit versions of VB. Only the module declarations for the API call should need to be changed. If you are interested in seeing this code, please contact us at

In this Issue

In this issue we are going to introduce a object class called cListBoxDragDrop. You can use this class to allow your end-users to rearrange the data in a standard Windows list box.

cListBoxDragDrop Class

Add drag and drop rearranging/sorting to a standard Windows list box. There are several important methods in the class.


FormObj - The form that contains the list box to this property (see Form_Load in the Sample Form)
ListBoxObj - The list box on which drag and drop actions should be performed.


MouseDown - Should be put in the MouseDown event of the list box.
MouseUp - Should be put in the MouseUp event of the list box.
DoDragDrop - Should be put in the MouseMove event of the list box with the X, Y values as parameters.
NOTE: Since a class can not trap events of a control in VB 4.0, you put these method calls in the events of the control and they perform the functions necessary to implement the drag and drop actions.

Sample Call/Form

To use this example, create a new project in Visual Basic 4.0 16-bit and drop a list box on a form. Change the Name property of the list box to lstWords. Paste the following code into the declarations section of the form.

Option Explicit

Dim oListDrag As New cListBoxDragDrop

Private Sub Form_Load()
    Set oListDrag.FormObj = Me
    Set oListDrag.ListBoxObj = lstWords
End Sub

Private Sub lstWords_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
    ' Traps the Mouse Down event which will start
    ' the dragging operation.
End Sub

Private Sub lstWords_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
    ' Traps the MouseUp event which will end
    ' the dragging operation.
End Sub

Private Sub lstWords_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
    ' Traps the MouseMove event which will show the
    ' item in the list being dragged around.
    oListDrag.DoDragDrop x, Y
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

Sample Compiled Program

If you would like to download the compiled executable, point your browser to
This location contains the executable you can run provided you have Visual Basic 4.0 (16-bit) installed on your system or have previously installed a VB 4.0 application. The only run-time file requirement should be VB40016.DLL.


That concludes our first issue of COTW. We hope you find the source code useful in your development.

The below describes the ways you can supply us some feedback about COTW. We would like to see our members help mold COTW into the best Visual Basic source code resource available. But to do that we need your feedback about what you like and what you do not like.

How to tell us what you think

If you have any suggestions for topics you would like to see covered in COTW, please email them to or use online feedback form at

If you have any source code you would like to submit for possible inclusion in COTW, please fill out our online submission form at

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945

About Code of the Week and How to get your own subscription

If you have received this issue in a free offer, please check out to order your own subscription to COTW. For a limited time you can get 52 issues of COTW for only $19.95. This is a full year of Visual Basic source code and information to help with all your development. So don't wait, subscribe now!