Home » Controls » How to Sort Numbered Items In List Box/Combo Box
How to Sort Numbered Items In List Box/Combo Box
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 'Controls - How to Sort Numbered Items In List Box/Combo Box Option Explicit 'If you place the items File1.gif, File2.gif, File3.gif and File10.gif in List Box or 'Combo Box, and set the Sorted property to True, they will be listed as shown below: ' 'File1.gif 'File10.gif 'File2.gif 'File3.gif ' 'Instead of this way: ' 'File1.gif 'File2.gif 'File3.gif 'File10.gif ' 'To sort them as shown above. ' 'Add a Command Button and a List Box to your form. Sub ReSort(lst As Control) Dim P%, PP%, C%, Pre$, S$, V&, NewPos%, CheckIt% Dim TempL$, TempItemData&, S1$ For P = 0 To lst.ListCount - 1 S = lst.List(P) For C = 1 To Len(S) V = Val(Mid$(S, C)) If V > 0 Then Exit For Next If V > 0 Then If C > 1 Then Pre = Left$(S, C - 1) NewPos = -1 For PP = P + 1 To lst.ListCount - 1 CheckIt = False S1 = lst.List(PP) If Pre <> "" Then If InStr(S1, Pre) = 1 Then CheckIt = True Else If Val(S1) > 0 Then CheckIt = True End If If CheckIt Then If Val(Mid$(S1, C)) < V Then NewPos = PP Else Exit For End If Next If NewPos > -1 Then TempL = lst.List(P) TempItemData = lst.ItemData(P) lst.RemoveItem (P) lst.AddItem TempL, NewPos lst.ItemData(lst.NewIndex) = TempItemData P = P - 1 End If End If Next Exit Sub End Sub Private Sub Command1_Click() Call ReSort(List1) End Sub Private Sub Form_Load() 'add items to the List Box List1.AddItem "File3.gif" List1.AddItem "File2.gif" List1.AddItem "File10.gif" List1.AddItem "File1.gif" End Sub |
Enjoy this article?
Filed under: Controls
Leave a comment