If IsNumeric(Index) = False Then 'Non-numeric index
Err.Raise 102, "ListClass", "Non-numeric index"
RaiseEvent Error(102, "Non-numeric index")
Exit Sub
End If
If Index = 0 Then 'Remove the first item
For i = 1 To Occur(sList)
AddToString Tmp, ReadLine(sList, i)
Next i
ElseIf Index < 0 Then 'Invalid index
ElseIf Index > 0 Then 'Remove a later item
For i = 0 To Index - 1
AddToString Tmp, ReadLine(sList, i)
Next i
If Index < Occur(sList) Then 'The removed item isn't the last item in the list
For i = Index + 1 To Occur(sList)
AddToString Tmp, ReadLine(sList, i)
Next i
End If
End If
sList = Tmp 'Save
Tmp = "" 'Clear up
RaiseEvent ItemRemove(Index)
End Sub
'Clear the list
Sub Clear()
sList = ""
RaiseEvent Clear
End Sub
'Get the text of a list item
'Set Index=-1 to get the complete list
Function Text(Optional Index As Long = -1) As String
If Index = vbNullString Then
Text = ""
Exit Function
End If
If IsNumeric(Index) = False Then 'Non-numeric index
Err.Raise 102, "ListClass", "Non-numeric index"
RaiseEvent Error(102, "Non-numeric index")
Text = ""
Exit Function
End If
If Index >= 0 Then Text = ReadLine(sList, Index)
If Index = -1 Then Text = sList
End Function
'Get the number of items
Function ListCount()
ListCount = Occur(sList) + 1
End Function
'Sort the list
Function Sort(Optional CapsFirst As Boolean = True, Optional ReverseOrder As Boolean = False) As Double
Dim tSort() As String
Dim Result As String, OldList As String
Dim SortTime As Double
sw_Reset 'Reset the stopwatch
If Trim(sList) = "" Then GoTo Pointless 'Check if the list string is not empty
If Occur(sList) = 0 Then GoTo Pointless 'Check if there really is a multi-line string, not just a single-line string
Result = "" 'Make sure the result buffer is empty
tSort = Split(sList, vbCrLf) 'Create the array
Result = PrettySort(tSort, CapsFirst, ReverseOrder) 'Sort and store in a temporary buffer
OldList = sList 'Store the old list (unsorted). In case of an error, this is restored, so the list won't be lost
If Trim(Result) <> "" Then
sList = Result 'Apply
Result = "" 'Clean the temporary buffer
OldList = "" 'Clean the old list buffer
SortTime = sw_Elapsed 'Return elapsed time since sort start
Sort = SortTime
RaiseEvent Sort(SortTime, True)
Exit Function
Else
SortTime = sw_Elapsed
RaiseEvent Sort(SortTime, False)
End If
Pointless: 'Unnecessary to sort, but not error worthy
Sort = 0
Exit Function
FndErr: 'An error occurred
RaiseEvent Sort(0, False)
Err.Raise 101, "ListClass", "Sorting failed"
RaiseEvent Error(101, "Sorting failed")
sList = OldList 'Restore the old list (in case the corrupted sorted list has been applied)
Sort = 0
End Function
'Put all items in a control.
'Currently there is only support for Textbox, Listbox and Combobox controls, but you can easily add support for more controls
Sub PutInControl(Ctl As Control, Optional AutoClean As Boolean = True)
Dim i As Long
'Auto clean (if true)
If AutoClean = True Then
If TypeOf Ctl Is TextBox Then Ctl.Text = "" 'Textbox
If TypeOf Ctl Is ListBox Or TypeOf Ctl Is ComboBox Then Ctl.Clear 'Listbox/Combobox
End If
For i = 0 To Occur(sList) 'Loop through all items
If TypeOf Ctl Is TextBox Then 'Using a textbox
If Trim(Ctl.Text) = "" Then Ctl.Text = ReadLine(sList, i) Else Ctl.Text = Ctl.Text & vbCrLf & ReadLine(sList, i)
ElseIf TypeOf Ctl Is ListBox Or TypeOf Ctl Is ComboBox Then 'Using a listbox or combobox (doesn't matter in this case, because they both have the necessary functions)
Ctl.AddItem ReadLine(sList, i)
End If
Next i
RaiseEvent ListCreated(Ctl)
End Sub
'Search for an item in the list
Function SearchItem(Expression As String, Optional ExactMatch As Boolean = False, Optional CaseMatch As Boolean = False) As Long
Dim i As Long
Dim Tmp(0 To 1) As String
If CaseMatch = True Then Tmp(1) = Expression Else Tmp(1) = LCase(Expression)
For i = 0 To Occur(sList) 'Loop through the list
Tmp(0) = ReadLine(sList, i) 'Read the current line
If ExactMatch = True Then 'Exact match required
If Tmp(0) = Expression Then 'Match
SearchItem = i
RaiseEvent SearchFinish(i)
Exit Function
Else 'No match
GoTo TryNext
End If 'Match
Else 'Exact match not required
If InStr(1, Tmp(0), Expression) > 0 Then 'Match
SearchItem = i
RaiseEvent SearchFinish(i)
Exit Function
Else 'No match
GoTo TryNext
End If
End If 'ExactMatch
TryNext:
Next i
'The code hereunder will only be executed if there are no results
SearchItem = -1
RaiseEvent SearchFinish(-1)
End Function
'Export the list to a file
Sub Export(Filename As String)
Dim FF As Long
FF = FreeFile
Open Filename For Binary Access Write As #FF 'Open the file for writing
Put #FF, , sList 'Write to the file
Close #FF 'Close the file
RaiseEvent ExportDone(Filename, Len(sList))
End Sub
'Import list from a file
Sub Import(Filename As String)
Dim FF As Long
FF = FreeFile
Open Filename For Binary Access Read As #FF 'Open the file for reading