File drag and drop not working on listbox

前端 未结 1 1512
-上瘾入骨i
-上瘾入骨i 2020-12-21 18:44

This is the first time I am working with drag and drop. So I have a form with a listbox and nothing else. I would like to be able to drag and drop files from de

相关标签:
1条回答
  • 2020-12-21 19:15

    I make this and i think this will be OK.And no need DragOver.

        private void listBox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.All;
            else
                e.Effect = DragDropEffects.None;
        }
    
        private void listBox_DragDrop(object sender, DragEventArgs e)
        {
            if (listBox.Items.Count != 0)
            {
                listBox.Items.Clear();
            }
            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            int i;
            for (i = 0; i < s.Length; i++)
                listBox.Items.Add(Path.GetFileName(s[i]));
        }
    
    0 讨论(0)
提交回复
热议问题