Results 1 to 10 of 10

Thread: [RESOLVED] Saving the Contents of a Listview

  1. #1

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Resolved [RESOLVED] Saving the Contents of a Listview

    Im unsure of what is the most efficient way to save the contents of a list view. Then when the form loads again i need it to read from the database file or where ever it is. It ha to save the name and description as the list view has 2 column headers. Any thought of how or to what type of file i should save it to i would be grateful

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

    Re: Saving the Contents of a Listview

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

  3. #3

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Saving the Contents of a Listview

    kk thanks a bunch ill test it and see if it works

  4. #4
    Hyperactive Member
    Join Date
    May 2006
    Posts
    389

    Re: Saving the Contents of a Listview

    oh it will... he gave me the same source code for the same question like 8 months ago...
    Visual Basic Rules!!!!!

  5. #5

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Saving the Contents of a Listview

    Kk though i was looking to write more to a *.adb file rather than a plain *.txt file but at the moment i just need to get the program in working order so this will do for now ill figure the rest out another time

  6. #6

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Saving the Contents of a Listview

    Quote Originally Posted by RhinoBull
    What's an "*.adb" ?
    Probably a text file to which he wishes to assign his own extension.

  8. #8

    Thread Starter
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Saving the Contents of a Listview

    Im not to sure myself though i remember seeing it done on my mates project it is called an ADB file thats all i know. I thought at first it mean Access Data Base but that is just a guess but anyway what Rhino gave me works fine

  9. #9
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: [RESOLVED] Saving the Contents of a Listview

    Originally posted by Hack
    Probably a text file to which he wishes to assign his own extension.
    Its an Office Suite, maybe this will help with the .adb extension

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

    Re: [RESOLVED] Saving the Contents of a Listview

    Hmmm. Well, I don't see any points using third party soft just to store few things - unless you need to store more than what you've mentioned text file is more than enough. Otherwise consider using MS Access database or at least stay away from any proprietary formats that nobody's using.

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