0

I have a WinForms application that displays the files inside some folder in a listbox (let's say D:\work). I need to add drag-n-drop property to my application, so that I can drag some files and drop it on my desktop.

So I managed to do that, but I want my form to minimize or better, sent to back (if there is an opened window to drop the file in it.

My listBox_mouseDown:

private void listBox_MouseDown(object sender, MouseEventArgs e)
{
    ListBox listBox = (ListBox)sender;
    int index = listBox.IndexFromPoint(e.Location);
    if (index >= 0)
    {
        Item selItem = (Item)listBox.Items[index];
        if (selItem != null)
        {
            if (e.Button == MouseButtons.Left)
            {
                clickPoint = Cursor.Position;
                string[] filesToDrag = { selItem.path };
                listBox.DoDragDrop(new DataObject(DataFormats.FileDrop, filesToDrag), DragDropEffects.Copy);// Here I want the window to minimize
                diff.X = Math.Abs(clickPoint.X - Cursor.Position.X);
                diff.Y = Math.Abs(clickPoint.Y - Cursor.Position.Y);
                if (diff.X < 20 && diff.Y < 20)
                    listBox_MouseClick(sender, e);
            }
            else
            {
                if (e.Button == MouseButtons.Right)
                {
                    selItem = (Item)listBox.Items[listBox.IndexFromPoint(e.X, e.Y)];
                    if (selItem.isFile() && !listBoxFavorites.Items.Contains(selItem))
                        listBoxFavorites.Items.Add(selItem);
                }
            }
        }
    }
}

listBox_DragOver:

private void listBox_DragOver(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Copy;  
}

Thanks in advance! :)

ivayle
  • 1,070
  • 10
  • 17
wael razouk
  • 88
  • 2
  • 8
  • What do you mean with "if there is an opened window to drop my file in" ? – GuidoG Nov 28 '16 at 15:06
  • Minimizing your own window does not accomplish much. The existing UX for drag+drop is to hover the drag cursor over the taskbar button or the "show desktop" link on the taskbar. Try it. – Hans Passant Nov 28 '16 at 15:46

1 Answers1

0

To minimize your form do this:

this.WindowState = FormWindowState.Minimized;

To send it to the back you might want to look at this: Setting a Windows form to be bottommost

Community
  • 1
  • 1
MrApnea
  • 1,776
  • 1
  • 9
  • 17