Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

Little String List Mod

$
0
0
Hi,

Here is some code I made to allow you to use the default VB Collection object to load and save strings it also got functions to find a string and more hope you find it usfull.

Code:

Private TList As New Collection

Public Sub Add(S As String)
    'Add item to the list obj
    Call TList.Add(S)
End Sub

Public Function Text() As String
Dim I As Long
Dim Buffer As String
    'Convert list to a string
    For I = 1 To TList.Count
        Buffer = Buffer & TList.Item(I) & vbNewLine
    Next I
    Text = Buffer
End Function

Public Function Remove(Index As Integer) As Boolean
On Error GoTo TDelErr:
    'Remove item from list
    Call TList.Remove(Index)
    Remove = True
    Exit Function
TDelErr:
    Remove = False
End Function

Public Function IndexOf(S As String) As Long
Dim I As Integer
Dim ndx As Long
    ndx = -1
    'Return a none mines number if S is found in the list
    For I = 1 To Count
        'Check items
        If (TList.Item(I) = S) Then
            ndx = I
            Exit For
        End If
    Next I
   
    IndexOf = ndx
End Function

Public Function Exsits(S As String) As Boolean
    'Lok for an item in the list
    Exsits = IndexOf(S) > -1
End Function

Public Function Item(Index As Integer) As String
On Error Resume Next
    'Return a item from the list at Index
    Item = TList.Item(Index)
End Function

Public Function LoadFromFile(Filename As String) As Boolean
Dim fp As Long
Dim sLine As String
On Error GoTo TFileErr:
   
    'Create new object
    Set TList = New Collection
    'Get free file
    fp = FreeFile
    'Open the file for reading
    Open Filename For Input As #fp
        Do While Not EOF(fp)
            'Read in one line at a time
            Line Input #fp, sLine
            'Add to list
            Call TList.Add(sLine)
        Loop
        'Close file
    Close #fp
   
    LoadFromFile = True
    Exit Function
TFileErr:
    LoadFromFile = False
End Function

Public Function SaveToFile(Filename As String) As Boolean
Dim fp As Long
On Error GoTo TFileErr:
   
    'Get free file
    fp = FreeFile
    'Open the file for output
    Open Filename For Output As #fp
        'Print all the items in the list using the Text Function
        Print #fp, Text
        'Close file
    Close #fp
   
    SaveToFile = True
    Exit Function
TFileErr:
    SaveToFile = False
End Function

Public Property Get Count() As Long
    'Return list count
    Count = TList.Count
End Property

Public Sub Clear()
    Set TList = New Collection
End Sub


Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>