Live Chat Icon For mobile
Live Chat Icon

How can I drag file names from Windows Explorer and drop them into a listbox

Platform: WinForms| Category: ListBox

Place a ListBox on your form, set its AllowDrop property and handle both DragEnter and DragDrop as below.


private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.All;
    else
        e.Effect = DragDropEffects.None;
}

private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
    string[] files = (string[])e.Data.GetData('FileDrop', false);
    foreach (string s in files)
    {
        //just filename 
        listBox1.Items.Add(s.Substring(1 + s.LastIndexOf(@'\')));

        //or fullpathname 
        //     listBox1.Items.Add(s); 
    }
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.