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
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?
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
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
Re: retaining values in a listbox?
Here is a routine that will do both.
VB Code:
Option Explicit
Private Sub SaveLoadListbox(lstLB As ListBox, sFilename As String, sSaveLoad As String)
Dim sText As String
Dim i As Long
Select Case sSaveLoad
Case "save"
Open sFilename For Output As #1
For i = 0 To lstLB.ListCount - 1
lstLB.Selected(i) = True
Print #1, lstLB.List(lstLB.ListIndex)
Next
Close #1
Case "load"
lstLB.Clear
Open sFilename For Input As #1
While Not EOF(1)
Line Input #1, sText
lstLB.AddItem sText
Wend
Close #1
End Select
End Sub
Private Sub cmdLoad_Click()
Call SaveLoadListbox(List1, "c:\Listbox.txt", "load")
End Sub
Private Sub cmdSave_Click()
Call SaveLoadListbox(List1, "c:\Listbox.txt", "save")
End Sub
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?
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.