How to find a song by name and remove it from playlist

260 views Asked by At

Basically I'm making a jukebox and I have a big problem with the WMPLib library.
I am trying to remove an item from the playlist , but the problem is that I can't find any way to get the index or the actual media by name.
I was thinking of making a replica array of the playlist , but that is just stupidly more work then what I should put into a simple task like that.

 private void queue_listbox_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        if (queue_listbox.SelectedItem != null)
        {
            wplayer.currentPlaylist.removeItem('Insert code here');
            music_listbox.Items.Add(queue_listbox.SelectedItem);
            queue_listbox.Items.Remove(queue_listbox.SelectedItem);
        }
    }
1

There are 1 answers

0
AustinWBryan On

Just use a Dictionary<TKey, TValue> like so:

// The string will be used as the name to find the song. 
// I don't know what type you are using for the song, 
// so I'm just using the madeup type "Song"
Dictionary<string, Song> songs = new Dictionary<string, Song>();

And then you can remove it by doing (pseudo code)

// Removes "Look at this Photograph" from the songs Dictionary
songs["Look at this Photograph - Nickleback"].Remove();

Dictionaries are pretty much lists, which are pretty much arrays, except, instead of passing a number as an indexer (songs[5] gets the 6th song) you can pass whatever type TKey is (songs["Look at this Photograph"] gets the song that is paired with that string).

I don't know if you made that Listbox class or if it's built by Microsoft. If it is built, then Items is can't be made a dictionary if it's already an array, in which case, you'll need to get more creative. You might have to basically recreate Listbox just to have the Dictionary or some clever workaround.

You could do something like making a ListboxManager which would hold a Dictionary<Listbox, Dictionary<string, Song>> and use an extension method called GetItems(this Listbox listbox) => ListBoxManager.Listboxes[listbox]; which would probably work.