Search code examples
c#.netlistboxaxwindowsmediaplayer

How to drag and drop mp3 files to a listbox without the file path?


I am having some problems with a media player made in C#, I had already asked for another problem here in stackoverflow, but now I have a different one and I think it's the last one.

I want to drag and drop MP3 and WAV files to a listbox in a windows form, I can drop them correctly, but I can't play them without showing the full path of the file.

Here's what I've done so far:

private void listBox1_DragDrop(object sender, DragEventArgs e)
    {

        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
        foreach (string m in files)
        {
            listBox1.Items.Add(Path.GetFileNameWithoutExtension(m));
            mediaList.Add(m);
        }

mediaList is a list that helps me to save the playlist. listbox1 is my main playlist.

To play the songs I use this code, which has been working well 'til now:

axWindowsMediaPlayer1.URL = mediaList[listBox1.SelectedIndex];

Thanks in advance. -ChrisCreateBoss


Solution

  • Listbox items can contain objects. Since you want to have access to the fullname, but display only the filename, this class will store the full name dropped, but only display the filename portion. These are handy when there are several bits of info to save for each item.

    class FileItem
    {
        public string FullName { get; set; }
        public FileItem(string file)
        {
            FullName = file;
        }
    
        public override string ToString()
        {
            return Path.GetFileNameWithoutExtension(FullName);
        }
    }
    

    The entire name is saved, but only the short name will display using the override. When files are dropped, we will create FileItem objects from them. Then there are 2 choices:
    a) place the objects in the ListBox and get rid of mediaList, or
    b) put the new items in medialist and use it as a DataSource

    This will do the second since the mediaList is not glued to the UI. Be sure to change mediaList as shown:

    // new mediaList 
    List<FileItem> mediaList = new List<FileItem>();
    ...
    private void lb1_DragDrop(object sender, DragEventArgs e)
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
        foreach (string f in files)
        {
            mediaList.Add(new FileItem(f));
        }
        // tell the LB to display whatever is in mediaList
        lb1.DataSource = mediaList;
    }
    

    Rather than 2 copies of the files, whats in mediaList is also displayed in the ListBox; the FileItem objects in it will display just the short name. Your original code should work with a tweak:

    axWindowsMediaPlayer1.URL = mediaList[lb1.SelectedIndex].FullName;
    

    Otherwise without the DataSource, you will need to cast Items back to a FileItem and use FullName to get the original full path name back:

    FileItem fil = (FileItem) lb1.Items[lb1.SelectedIndex];
    axWindowsMediaPlayer1.URL = fil.FullName;
    
    // or 
    axWindowsMediaPlayer1.URL = ((FileItem)lb1.SelectedItem).FullName;