I have a application that uses the ListView object. I would like the values it contains to be retained until the next time the user starts up the program. The values may be removed and added in run-time by the user. How would I do this?
Printable View
I have a application that uses the ListView object. I would like the values it contains to be retained until the next time the user starts up the program. The values may be removed and added in run-time by the user. How would I do this?
Try the following:
VB Code:
'to save to a file (can also be done during form_unload or queryunload) Private Sub btnSave_Click() '=========================== Dim i%, j% Dim strHeader As String Dim strLine As String Open App.Path & "\listview.txt" For Output As #1 With ListView1.ListItems strHeader = "Headers:" For j = 1 To ListView1.ColumnHeaders.Count strHeader = strHeader & ListView1.ColumnHeaders(j) & ";" Next j Print #1, Left(strHeader, Len(strHeader) - 1) For i = 1 To .Count strLine = strLine & .Item(i).Text & ";" For j = 1 To ListView1.ColumnHeaders.Count - 1 strLine = strLine & .Item(i).SubItems(j) & ";" Next j Print #1, Left(strLine, Len(strLine) - 1) strLine = "" Next i End With Close #1 End Sub 'to load from a file (can be done during form_load as well) Private Sub Command2_Click() '============================= Dim pos%, i%, j%, strLine$ Dim varAllValues As String Dim MyValues() As String ListView1.ColumnHeaders.Clear ListView1.ListItems.Clear If Dir(App.Path & "\listview.txt") = "" Then Exit Sub Open App.Path & "\listview.txt" For Input As #1 Do While Not EOF(1) Line Input #1, strLine If Not InStr(1, LCase(strLine), "headers") > 0 Then MyValues() = Split(strLine, ";", , vbTextCompare) Set itm = ListView1.ListItems.Add(, , MyValues(0)) For j = 1 To UBound(MyValues) Set LSI = itm.ListSubItems.Add(, , MyValues(j)) Next j Else If InStr(1, LCase(strLine), "headers:") > 0 Then pos = InStrRev(strLine, ":") strLine = Mid(strLine, pos + 1) MyValues() = Split(strLine, ";", , vbTextCompare) With ListView1.ColumnHeaders For j = 0 To UBound(MyValues) .Add , , MyValues(j) Next j End With End If End If Loop Close #1 End Sub
Thank you.
You're welcome.