Results 1 to 7 of 7

Thread: retaining values in a listbox?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    29

    retaining values in a listbox?

    I know that values can be written to and read from the registry, is it also possible to do this with the contents of a listbox?

    The listbox contains a list of filenames and paths like eg. below:

    c:\media\song1.mp3
    c:\media\song2.mp3

    etc.


    The list contents are not fixed, they may be altered by the user when running.

    Does anybody know how i can do this?

    Thanks

    Tom

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

    Re: retaining values in a listbox?

    Hmmm...yes, you could do that, but I'm not real sure you would want to.

    I gather you want to ensure the listbox contains the same items each time the program is run, is that correct?

  3. #3
    Lively Member malb's Avatar
    Join Date
    Feb 2005
    Location
    England
    Posts
    88

    Re: retaining values in a listbox?

    Save the contents of the list to a text file or an ini file? Then you can load up the backup when your applicaation runs and re-populate the list with the saved data. No need to necasarily save to the regestry.

    hope this helps
    You fear me because I'm different, i pitty you because your all the same.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    29

    Re: retaining values in a listbox?

    It's for a very basic media playlist
    I've got it set up so that by clicking on item in a filelist and clicking the "add" button does this:
    [vb]
    Private Sub cmdAdd_Click()
    Tracklist.AddItem ("c:\media\" & File1.FileName)

    End Sub
    [/vb]

    All i really want to do is have a button that saves the list (if it has to be as a txt file then so be it) but i want to be able to open the list back up when the form loads and have the items put back exactly in the list box. Just in case it's relevant i'm sequentially setting the URL of the WindowsMediaPlayer component to the tracklist.text so it plays the listed file, this is why having them in a list opposed to another control is important.


    Thanks

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

    Re: retaining values in a listbox?

    Here is a routine that will do both.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub SaveLoadListbox(lstLB As ListBox, sFilename As String, sSaveLoad As String)
    4. Dim sText As String
    5. Dim i As Long
    6. Select Case sSaveLoad
    7.    Case "save"
    8.     Open sFilename For Output As #1
    9.     For i = 0 To lstLB.ListCount - 1
    10.         lstLB.Selected(i) = True
    11.         Print #1, lstLB.List(lstLB.ListIndex)
    12.     Next
    13.     Close #1
    14.  
    15.    Case "load"
    16.    lstLB.Clear
    17.     Open sFilename For Input As #1
    18.     While Not EOF(1)
    19.       Line Input #1, sText
    20.       lstLB.AddItem sText
    21.     Wend
    22.     Close #1
    23. End Select
    24.  
    25. End Sub
    26.  
    27. Private Sub cmdLoad_Click()
    28. Call SaveLoadListbox(List1, "c:\Listbox.txt", "load")
    29. End Sub
    30.  
    31. Private Sub cmdSave_Click()
    32. Call SaveLoadListbox(List1, "c:\Listbox.txt", "save")
    33. End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    29

    Re: retaining values in a listbox?

    Fantastic, that works brilliant, thanks

    I assume if i wanted to store several different list boxes, i could just use a string to replace the path in that code?

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

    Re: retaining values in a listbox?

    Quote Originally Posted by UKDJF
    Fantastic, that works brilliant, thanks

    I assume if i wanted to store several different list boxes, i could just use a string to replace the path in that code?
    Sure.

    You could also add something like pstrFilePath As String as another parameter to the sub, and pass the path along with the file name and either load or save.

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