Visual Basic Code of the Week (COTW)
Issue #67
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1998 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Happy Holidays

We wish everyone a happy holiday season. Code of the Week will not be publishing an issue this coming weekend. All subscribers will automatically have their subscription extended by one issue (including Free Trial subscribers).

Free Software

The Free Software contest ends in less than 10 days, if you have not already entered go to now!

In this Issue

This issue we are going to do something a little different with Code of the Week. We have designed a UserControl that extends the ListBox built-in control with features to reorder the list using an up/down button or drag and drop operations. The source code is available at and includes complete source code and a compiled OCX. The downloadable source code also includes a 32-bit version of Issue #1 (Drag and Drop in a ListBox).

This source code is designed for VB 5.0 and up. Questions? Email us at


The UserSortableListBox is a VB5 or VB6 user control. User controls are one of the greatest features of VB5/6. It allows you to extend or create your own controls. This project takes an ordinary list box and gives it features to move items up and down in the list. You can see many uses for this in applications that allow you to select the position of columns in a report or the appearance of fields in a grid. We think you will find many uses for this type of control.

All the standard properties for a listbox are exposed for your use (such as List, ItemData, Clear, etc). The only one not exposed is Sorted which would not make any sense in this control anyway.


Public Property Let ButtonsVisible(sVisible As Boolean)
Public Property Get ButtonsVisible() As Boolean
Public Function AddItemExtended(sItem As String, lItemData As Long) As Variant

ButtonsVisible is a property that controls the visibility of the Up/Down buttons that appear near the bottom of the listbox. It is a Read/Write property. You can control this property at design-time or run-time. Sample usage would be ctl.ButtonsVisible = True.

AddItemExtended allows you to add an item to the listbox and its associated item data value in one call. This simplifies the usage of a list box when you use the item data feature.


Use the List and ItemData arrays to retrieve the items from the listbox the same way you would with a standard listbox.

Sample Usage

Below is an example of how this control works. It is assumed that you have put a UserSortableListbox on a form and called it uslstFields. It also assumes you have added a button called Command1.

'  This routine will get the data from the listbox in the order
'  that it appears in the listbox.
Private Sub Command1_Click()
    Dim x As Integer

    For x = 0 To uslstFields.ListCount - 1
        Debug.Print uslstFields.List(x), uslstFields.ItemData(x)
End Sub

'  This form loads some data in form load to see how the
'  AddItemExtended subroutine works.  Notice that
'  there are two parameters.
Private Sub Form_Load()
    uslstFields.AddItemExtended "Item 1", 1
    uslstFields.AddItemExtended "Item 2", 2
    uslstFields.AddItemExtended "Item 3", 3
    uslstFields.AddItemExtended "Item 4", 4
    uslstFields.AddItemExtended "Item 5", 5
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:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

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