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).
The Free Software contest ends in less than 10 days, if you have not already entered go to http://www.codeoftheweek.com/contest.html now!
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 http://www.codeoftheweek.com/membersonly/bi/issue67 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 questions@codeoftheweek.com.
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.
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) Next 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.Clear 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
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: http://www.codeoftheweek.com/membersonly/bi/0067.html