[VB.NET] ListBox Save?

Jordy1212
Jordy1212 used Ask the Experts™
on
Hi, I'm trying to make a listbox to save a list of songs. how do I do this? This is the code I'm using:
 
For Each Item As In lstMusic.Items
                My.Settings.SaveMusic = Item
                MsgBox(String.Format("You've just saved {0} songs", MsgBoxStyle.Information, "Music Saved!", lstMusic.Items.Count))
            Next

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006

Commented:
do you need to As in your for each

also should you be using My.Settings? I thought that was really for application settings

do you want to save this in a kind of playlist for future reference? or just during the duration of your app's session
Top Expert 2006

Commented:
Also, the other thing wrong would be where you put the listcount
it should be after the message

        For Each Item In lstMusic.Items
            My.Settings.SaveMusic = Item
        Next
        MsgBox(String.Format("You've just saved {0} songs", lstMusic.Items.Count, MsgBoxStyle.Information, "Music Saved!"))

Top Expert 2006

Commented:
Regarding My.Settings. Is this set as User or Application level? Thats what I meant earlier. If at Application then that should be for application settings. User settings are not saved as far as I know and to save a playlist, being a user specific thing should not really be saved under Application level.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Author

Commented:
The scope is under user

Author

Commented:
Also, I want it to save so when you open the application all of the items/songs (in this case) will still be there.
Top Expert 2006

Commented:
I realised something. Do get the list you have to keep appending SaveMusic. You can use a delimiter like ; or ,

Author

Commented:
I'm not sure what you mean by that.
If you have a list of items to add, change the type of your setting to System.Collections.Specialized.StringCollection. You can then add your items like this....

        Dim coll As New System.Collections.Specialized.StringCollection
        For Each item As String In lstMusic.Items
            coll.Add(item)
        Next
        My.Settings.SaveMusic = coll

Wayne
Top Expert 2006
Commented:
ok, have a look at this then



Dim savedMp3 As String

        For Each mp3 In lstMusic.Items
            savedMp3 = savedMp3 & ";" & mp3
        Next mp3

        savedMp3 = savedMp3.Substring(2)
        My.Settings.SaveMusic = savedMp3

        MsgBox(String.Format("You've just saved {0} songs", lstMusic.Items.Count, MsgBoxStyle.Information, "Music Saved!"))

Open in new window

Author

Commented:
Both of you helped me very much thanks! I added 250 points for the both of you
Top Expert 2006

Commented:
sorry missed your post
>> I'm not sure what you mean by that.

you probably know by now, but in case you didnt know what I was talking about, the original code kept overwrting SaveMusic with the last file so you only ever stored one mp3.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial