Results 1 to 4 of 4

Thread: [Resolved] Storing Values of a ListView

  1. #1

    Thread Starter
    Lively Member GoldEagle's Avatar
    Join Date
    Feb 2006
    Location
    Canada
    Posts
    82

    Resolved [Resolved] Storing Values of a ListView

    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?
    Last edited by GoldEagle; Apr 22nd, 2006 at 08:53 PM.
    If you found my post in anyway helpful please rate it.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Storing Values of a ListView

    Try the following:
    VB Code:
    1. 'to save to a file (can also be done during form_unload or queryunload)
    2. Private Sub btnSave_Click()
    3. '===========================
    4. Dim i%, j%
    5. Dim strHeader As String
    6. Dim strLine As String
    7.  
    8.     Open App.Path & "\listview.txt" For Output As #1
    9.         With ListView1.ListItems
    10.             strHeader = "Headers:"
    11.             For j = 1 To ListView1.ColumnHeaders.Count
    12.                 strHeader = strHeader & ListView1.ColumnHeaders(j) & ";"
    13.             Next j
    14.             Print #1, Left(strHeader, Len(strHeader) - 1)
    15.             For i = 1 To .Count
    16.                 strLine = strLine & .Item(i).Text & ";"
    17.                 For j = 1 To ListView1.ColumnHeaders.Count - 1
    18.                     strLine = strLine & .Item(i).SubItems(j) & ";"
    19.                 Next j
    20.                 Print #1, Left(strLine, Len(strLine) - 1)
    21.                 strLine = ""
    22.             Next i
    23.         End With
    24.     Close #1
    25.  
    26. End Sub
    27.  
    28. 'to load from a file (can be done during form_load as well)
    29. Private Sub Command2_Click()
    30. '=============================
    31. Dim pos%, i%, j%, strLine$
    32. Dim varAllValues As String
    33. Dim MyValues() As String
    34.  
    35.     ListView1.ColumnHeaders.Clear
    36.     ListView1.ListItems.Clear
    37.    
    38.     If Dir(App.Path & "\listview.txt") = "" Then Exit Sub
    39.    
    40.     Open App.Path & "\listview.txt" For Input As #1
    41.         Do While Not EOF(1)
    42.             Line Input #1, strLine
    43.             If Not InStr(1, LCase(strLine), "headers") > 0 Then
    44.                 MyValues() = Split(strLine, ";", , vbTextCompare)
    45.                 Set itm = ListView1.ListItems.Add(, , MyValues(0))
    46.                 For j = 1 To UBound(MyValues)
    47.                     Set LSI = itm.ListSubItems.Add(, , MyValues(j))
    48.                 Next j
    49.             Else
    50.                 If InStr(1, LCase(strLine), "headers:") > 0 Then
    51.                     pos = InStrRev(strLine, ":")
    52.                     strLine = Mid(strLine, pos + 1)
    53.                     MyValues() = Split(strLine, ";", , vbTextCompare)
    54.                     With ListView1.ColumnHeaders
    55.                         For j = 0 To UBound(MyValues)
    56.                             .Add , , MyValues(j)
    57.                         Next j
    58.                     End With
    59.                 End If
    60.                
    61.             End If
    62.         Loop
    63.     Close #1
    64.  
    65. End Sub

  3. #3

    Thread Starter
    Lively Member GoldEagle's Avatar
    Join Date
    Feb 2006
    Location
    Canada
    Posts
    82

    Re: Storing Values of a ListView

    Thank you.
    If you found my post in anyway helpful please rate it.

  4. #4

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