Tania Akter Tania Akter - 3 months ago 19
C# Question

Why is multiple item drop not working on listbox?

I create a playlist for media player.

Follow my code:

Xaml:

<MediaElement x:Name="mePlayer" Margin="64,0,90,61" ></MediaElement>
<ListBox x:Name="listbox4" Background="Salmon" BorderBrush="Black" BorderThickness="3" Drop="listbox4_Drop" >
</ListBox>
<Button x:Name="load" Content="Load" HorizontalAlignment="Left" VerticalAlignment="Top" Width="76" Click="load_Click" Margin="184,285,0,0"/>


Xaml.cs:

private Dictionary<string, string> fileDictionary = new Dictionary<string, string>();

private void load_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.DefaultExt = ".mp3";
ofd.Filter = "All|*.*";
ofd.Multiselect = true;
Nullable<bool> result = ofd.ShowDialog();
if (result == true)
{

for (int i = 0; i < ofd.FileNames.Length; i++)
{
var filePath = ofd.FileNames[i];
var fileName = System.IO.Path.GetFileName(filePath);
fileDictionary.Add(fileName, filePath);
listbox4.Items.Add(fileName);
listbox4.SelectedItem = fileName;
}
}
}


private void listbox4_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{

string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];

foreach (string droppedFilePath in droppedFilePaths)
{
for (int i = 0; i < droppedFilePaths.Length; i++)
{
var filePath = droppedFilePaths[i];
var fileName = System.IO.Path.GetFileName(filePath);
fileDictionary.Add(fileName, filePath);
listbox4.Items.Add(fileName);
listbox4.SelectedItem = fileName;
}

}
}
}


It's working single file drop but while I drop multiple file then it's not added on listbox.

Multiple loaded file is loaded but multiple file will not dropped.

How can I drop multiple file on listbox?

Answer

Since I am not able to replicate the problem you listed I currently can not help you in that regard. Though, I can help you where I see fit.

Your current Drop method has an extra loop that multiples the number of items you add to the listbox.

Your current method:

    private void listbox4_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {

            string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];

            foreach (string droppedFilePath in droppedFilePaths)
            {
                //if you keep this loop, you will all the dropped files for each dropped file
                //therefore, if I dropped 3 files, I'd get 9 entries in the listbox
                //if I dropped 4 files, I'd get 16 entries and so on...
                for (int i = 0; i < droppedFilePaths.Length; i++)//this has to go
                {//this has to go
                    var filePath = droppedFilePaths[i];//this needs to be a different variable since "i" will no longer exist
                    var fileName = System.IO.Path.GetFileName(filePath);
                    //fileDictionary.Add(fileName, filePath);
                    listbox4.Items.Add(fileName);
                    listbox4.SelectedItem = fileName;
                }//this has to go

            }
        }
    }

Refactored (using the ForEach)

    private void blaze_125_listbox4_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {

            string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];

            foreach (string droppedFilePath in droppedFilePaths)
            {
                var filePath = droppedFilePath;
                var fileName = System.IO.Path.GetFileName(filePath);
                //fileDictionary.Add(fileName, filePath);
                listbox4.Items.Add(fileName);
                listbox4.SelectedItem = fileName;
            }

        }
    }

This would also work (using the ForLoop)

    private void blaze_125_listbox4_Drop_anotherSpin(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];

            for (int i = 0; i < droppedFilePaths.Length; i++)
            {
                var filePath = droppedFilePaths[i];
                var fileName = System.IO.Path.GetFileName(filePath);
                //fileDictionary.Add(fileName, filePath);
                listbox4.Items.Add(fileName);
                listbox4.SelectedItem = fileName;
            }
        }
    }

Slimmer

    private void blaze_125_listbox4_Drop_Slimmer(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] droppedFilePaths =
                e.Data.GetData(DataFormats.FileDrop, true) as string[];

            foreach (string droppedFilePath in droppedFilePaths)
            {
                listbox4.Items.Add(System.IO.Path.GetFileName(droppedFilePath));
            }
        }
    }