Results 1 to 11 of 11

Thread: Useful ListBox, ComboBox and Array() Functions

Threaded View

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Useful ListBox, ComboBox and Array() Functions

    vb Code:
    1. Public Function ArrayInitialized(v_Array As Variant) As Boolean
    2. Dim X As Variant
    3.  
    4. On Error GoTo errHandler
    5.  
    6. X = UBound(v_Array)
    7.  
    8. ArrayInitialized = True
    9.  
    10. Exit Function
    11.  
    12. errHandler:
    13. ArrayInitialized = False
    14.  
    15. End Function
    16.  
    17.  
    18. ' Most functions listed here ensure you're sending a list to them.
    19. '  The next function does that.
    20.  
    21. Public Function IsList(List As Control) As Boolean
    22.  
    23. ' Determines if control is a list.
    24.  
    25. IsList = (TypeName(List) = "ListBox") Or (TypeName(List) = "ComboBox") Or (TypeName(List) = "FileListBox")
    26.  
    27. End Function
    28.  
    29. ' Takes everything in the list and puts it in an array.
    30. Public Function FillArrayFromList(List As Control, ByRef sArray() As String, Optional vItemData) As Long
    31. Dim n As Long
    32.  
    33. On Error GoTo errHandler
    34. ' Returns Error Code.
    35.  
    36. If Not IsList(List) Then Exit Function ' Return 0
    37.  
    38. If List.ListCount > 0 Then
    39.   ReDim sArray(List.ListCount - 1)
    40.   If Not IsMissing(vItemData) Then ReDim vItemData(List.ListCount - 1)
    41. Else
    42.   Erase sArray
    43.   If Not IsMissing(vItemData) Then Erase vItemData
    44.   Exit Function ' Return 0
    45. End If
    46.  
    47. For n = 0 To List.ListCount - 1
    48.  
    49.   sArray(n) = List.List(n)
    50.  
    51.   If Not IsMissing(vItemData) Then
    52.     vItemData(n) = List.ItemData(n)
    53.   End If
    54.  
    55. Next n
    56.  
    57. ' Return 0
    58.  
    59. Exit Function
    60.  
    61. errHandler:
    62. FillArrayFromList = Err
    63.  
    64. End Function
    65.  
    66. ' Puts only selected items in a multi-select list into an array.
    67. Public Function FillArrayFromListSelected(List As Control, ByRef sArray() As String) As Long
    68. Dim n As Long
    69. Dim j As Long
    70. Dim nCount As Long
    71.  
    72. ' Stuffs all selected items from a ListBox or ComboBox into an array.
    73.  
    74. On Error GoTo errHandler
    75. ' Returns Error Code.
    76.  
    77. nCount = List.SelCount
    78. If nCount = 0 Then Exit Function
    79.  
    80. ReDim sArray(nCount - 1)
    81.  
    82. For n = 0 To List.ListCount - 1
    83.   If List.Selected(n) Then
    84.     sArray(j) = List.List(n)
    85.     j = j + 1
    86.   End If
    87. Next n
    88.  
    89. ' Return 0
    90.  
    91. Exit Function
    92.  
    93. errHandler:
    94. FillArrayFromListSelected = Err
    95.  
    96. End Function
    97.  
    98. ' Fills a list from an array.
    99. Public Function FillListFromArray(List As Control, sArray() As String) As Long
    100. Dim n As Long
    101.  
    102. On Error GoTo errHandler
    103. ' Returns Error Code.
    104.  
    105. List.Clear
    106.  
    107. If Not ArrayInitialized(sArray) Then Exit Function ' Return 0
    108.  
    109. For n = LBound(sArray) To UBound(sArray)
    110.   List.AddItem sArray(n)
    111. Next n
    112.  
    113. List.ListIndex = -1
    114.  
    115. ' Return 0
    116.  
    117. Exit Function
    118.  
    119. errHandler:
    120. FillListFromArray = Err
    121.  
    122. End Function
    123.  
    124. ' This one works well when you're filling a list from a database and using the
    125. ' Primary key (Long integer) as the ItemData.
    126. Public Function ListIndexFromItemData(List As Control, ItemData As Long) As Long
    127. Dim n As Long
    128.  
    129. ' Returns the ListIndex of a List item when given ItemData.
    130.  
    131. On Error GoTo errHandler
    132.  
    133. ListIndexFromItemData = -1
    134.  
    135. If Not IsList(List) Then Exit Function ' Return -1
    136.  
    137. With List
    138.   For n = 0 To .ListCount - 1
    139.     If .ItemData(n) = ItemData Then
    140.       ListIndexFromItemData = n
    141.       Exit Function
    142.     End If
    143.   Next n
    144. End With
    145.  
    146. ' Return -1
    147.  
    148. Exit Function
    149.  
    150. errHandler:
    151. LogError Error, Err, List.Name, "bLists.ListIndexFromItemData()"
    152.  
    153. End Function
    154.  
    155. Public Function ItemDataFromListIndex(List As Control) As Variant
    156.  
    157. ' Returns the ItemData of a selected item in a ListBox or ComboBox.
    158.  
    159. On Error GoTo errHandler
    160.  
    161. ItemDataFromListIndex = 0
    162. If Not IsList(List) Then Exit Function
    163.  
    164. ItemDataFromListIndex = List.ItemData(List.ListIndex)
    165.  
    166. Exit Function
    167.  
    168. errHandler:
    169. LogError Error, Err, List.Name, "bLists.ItemDataFromListIndex()"
    170.  
    171. End Function
    172.  
    173. Public Sub MoveDownInList(ByRef List As Control)
    174. Dim iListIndex As Long
    175. Dim sItem As String
    176. Dim fSelected As Boolean
    177. Dim v_ItemData As Variant
    178.  
    179. ' Moves a list item down in the list by one item.
    180. ' Saves Selected status (Multi-Select lists) and ItemData.
    181.  
    182. If Not IsList(List) Then Exit Sub
    183.  
    184. ' If selected item is last in list then do nothing.
    185. If List.ListIndex = List.ListCount - 1 Then Exit Sub
    186.  
    187. With List
    188.   ' Save Selected property.
    189.   fSelected = .Selected(.ListIndex)
    190.  
    191.   iListIndex = .ListIndex
    192.   sItem = .List(.ListIndex)
    193.  
    194.   ' Save ItemData.
    195.   v_ItemData = .ItemData(.ListIndex)
    196.  
    197.   ' Remove item from list
    198.   .RemoveItem .ListIndex
    199.  
    200.   ' Place item back in list one index lower.
    201.   .AddItem sItem, iListIndex + 1
    202.  
    203.   ' Restore Selected property.
    204.   .Selected(.NewIndex) = fSelected
    205.  
    206.   .ListIndex = .NewIndex
    207.  
    208.   ' Restore ItemData.
    209.   .ItemData(.NewIndex) = v_ItemData
    210. End With
    211.  
    212. End Sub
    213.  
    214. Public Function MoveFromListToList(ByVal Index As Integer, ByRef FromList As Control, ByRef ToList As Control) As Long
    215.  
    216. On Error GoTo errHandler
    217.  
    218. ' Removes a selected item from one list and puts it in another list.
    219. ' Returns 0 if succesful or -1 if nothing moved.
    220.  
    221. MoveFromListToList = -1
    222.  
    223. If Index < 0 Then Exit Function ' Return -1
    224. If Not IsList(FromList) Then Exit Function ' Return -1
    225. If Not IsList(ToList) Then Exit Function  ' Return -1
    226.  
    227. With ToList
    228.   .AddItem FromList.List(Index)
    229.   .ItemData(.NewIndex) = FromList.ItemData(Index)
    230. End With
    231.  
    232. FromList.RemoveItem Index
    233.  
    234. ' Return 0
    235. MoveFromListToList = 0
    236.  
    237. Exit Function
    238.  
    239. errHandler:
    240.  
    241. MoveFromListToList = -1
    242.  
    243. End Function
    244.  
    245. Public Sub MoveUpInList(List As Control)
    246. Dim iListIndex As Long
    247. Dim sItem As String
    248. Dim fSelected As Boolean
    249. Dim v_ItemData As Variant
    250.  
    251. ' Moves a list item up in the list by one item.
    252. ' Saves Selected status (Multi-Select lists) and ItemData.
    253.  
    254. If Not IsList(List) Then Exit Sub
    255.  
    256. If List.ListIndex < 1 Then Exit Sub
    257.  
    258. With List
    259.   fSelected = .Selected(.ListIndex)
    260.  
    261.   iListIndex = .ListIndex
    262.   sItem = .List(.ListIndex)
    263.  
    264.   ' Save ItemData.
    265.   v_ItemData = .ItemData(.ListIndex)
    266.  
    267.   ' Remove item from list.
    268.   .RemoveItem .ListIndex
    269.  
    270.   ' Place item back in list one index higher.
    271.   .AddItem sItem, iListIndex - 1
    272.  
    273.   .Selected(.NewIndex) = fSelected
    274.   .ListIndex = .NewIndex
    275.  
    276.   ' Restore ItemData.
    277.   .ItemData(.NewIndex) = v_ItemData
    278. End With
    279.  
    280. End Sub
    281.  
    282. ' The next two are good for ensuring you flexgrids restore what the user set.
    283. Public Sub RestoreFlexGridColumnWidths(frm As Form, Flex As Control)
    284. Dim iCol As Integer
    285.  
    286. On Error Resume Next
    287.  
    288. If TypeName(Flex) <> "MSFlexGrid" Then Exit Sub
    289.  
    290. With Flex
    291.   .Redraw = False
    292.   For iCol = 0 To .Cols - 1
    293.     .ColWidth(iCol) = Registry.GetSetting("Windows", frm.Name & " Col " & iCol, 2400)
    294.   Next iCol
    295.   .Redraw = True
    296.   .row = 0
    297. End With
    298.  
    299. End Sub
    300.  
    301. Public Sub SaveFlexGridColumnWidths(frm As Form, Flex As Control)
    302. Dim iCol As Integer
    303.  
    304. If TypeName(Flex) <> "MSFlexGrid" Then Exit Sub
    305.  
    306. With Flex
    307.   .Redraw = False
    308.   For iCol = 0 To .Cols - 1
    309.     Registry.SaveSetting "Windows", frm.Name & " Col " & iCol, .ColWidth(iCol)
    310.   Next iCol
    311.   .Redraw = True
    312. End With
    313.  
    314. End Sub
    Last edited by cafeenman; Sep 21st, 2010 at 11:33 AM. Reason: Better thread title

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width