[RESOLVED] [2005] need help with listboxes
i'm making a program for a friend that is a DJ where people can request songs to the DJ and so it sends the requested songs to him.
and so i have two listboxes, the first one being the list of songs and the second one being the already requested songs. we don't want people to double-request songs so i want the entry to get removed from the first list box as it gets added to the second one. that part works. just in case they close the application, i made it save the list to a txt file and when it opens, it shows the list of already requested songs. the problem is that it shows the songs in the list of requested songs, but when it reloads the form it also re-adds the songs to the list of songs (since it reads off a certain folder).
hopefully you understood that.
here is my code:
VB Code:
'Load what is already requested
readrequested = New IO.StreamReader("RequestedList.txt")
While (readrequested.Peek() > -1)
ListBox2.Items.Add(readrequested.ReadLine)
ListBox1.Items.Remove(readrequested.ReadLine) 'Tried doing this but still does not remove the items, probably because it has multiple lines.
End While
readrequested.Close()
Re: [2005] need help with listboxes
When you request a song then the item is selected right?
The you could use this...(i think)
VB Code:
ListBox1.Items.Remove(ListBox1.SelectedItem)
but this only removes it from the listbox not the txt file
Re: [2005] need help with listboxes
You would be much better to use the Settings feature in .NET.
Check out this post: Settings
The description by JMC about MySettings will provide the info (mine simply tells you where it's located). It's very easy to use. I've just tried it and it's great. Just make sure you change the Type to a string collection.
You just create two variables in the Settings, one for your main music list and one for the requests. Add and remove from the variables and bind the listboxes to the settings on form load. might sound difficult but it's very easy.
Re: [2005] need help with listboxes
it doesn't work, since the text file/listbox2 has multiple lines, and the items in listbox1 are in different locations (not in the same order as the ones in listbox2)
Re: [2005] need help with listboxes
I did something like this for a PDA, where I could select items from a list, and show them on a different list, and wanted that different list shown on any subsequent loads. From there, the similarities end.
I decided to make a class that held the item, and it's selected state. Then I made a class that held a list of those item objects. To fill the list, I would just iterate through the list and look for selected items. When something was selected on one list or the other, it's selection state was toggled.
That seems to be what you are headed towards. You could fill the second list first, and as you fill the first one, look to see if the song is already in the second list. Or else you might find some advantage to holding the songs in a class, similar to what I had done, so that you can go once through the list, and put the song into whichever listbox is appropriate based on the selected member of the song object. If you were to do that, you might look at the Serializable() attribute as a really fast way to save the array of song objects.
Re: [2005] need help with listboxes
ok, i resolved it. for those who want to know what i did, this is it:
VB Code:
Dim varrequestedsongmax As Integer = ListBox2.Items.Count - 1
For varrequestedsongnumber As Integer = 0 To varrequestedsongmax Step 1
ListBox1.Items.Remove(ListBox2.Items.Item(varrequestedsongnumber))
Next varrequestedsongnumber