Drag and Drop, help please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #1

    Drag and Drop, help please

    I've got a program that requires that the user pick a file or folder on their computer. Currently the user clicks Open Single in the File menu and an openFileDialog comes up; to open a folder the user clicks Batch from the File menu and an folderBrowserDi alog comes up. I'd like to expand this to allow the user to simply drag a file or folder onto the program window, and I'm realizing how complicated drag n drop can be.

    I'm starting by just testing folders. I've set up an event handler for the dragEnter and dragLeave events. When I drag a folder over the form the dragOver event handler confirms that the data being dragged is actually a folder and returns either true or false. The dragLeave handler then displays all the files in that folder when I let go of the mouse. The problem here is that while letting go of the mouse completes the drag and drop, so does dragging my mouse outside the form's bounds. I need a way to confirm that the data was actually dropped onto the form.

    I've tried the DragDrop event, but it doesn't seem to do anything. I tried this simple code:

    Code:
    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        MessageBox.Show("Data was dropped");
    }
    then I tried dropping something onto the form. Nothing happened. I didn't get a messagebox, nothing.

    So how can I get the program to only process the information if the data was actually dropped onto the form, and not if it was dragged onto then out of the form?
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    Originally posted by HaLo2FrEeEk
    I've got a program that requires that the user pick a file or folder on their computer. Currently the user clicks Open Single in the File menu and an openFileDialog comes up; to open a folder the user clicks Batch from the File menu and an folderBrowserDi alog comes up. I'd like to expand this to allow the user to simply drag a file or folder onto the program window, and I'm realizing how complicated drag n drop can be.

    I'm starting by just testing folders. I've set up an event handler for the dragEnter and dragLeave events. When I drag a folder over the form the dragOver event handler confirms that the data being dragged is actually a folder and returns either true or false. The dragLeave handler then displays all the files in that folder when I let go of the mouse. The problem here is that while letting go of the mouse completes the drag and drop, so does dragging my mouse outside the form's bounds. I need a way to confirm that the data was actually dropped onto the form.

    I've tried the DragDrop event, but it doesn't seem to do anything. I tried this simple code:

    Code:
    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        MessageBox.Show("Data was dropped");
    }
    then I tried dropping something onto the form. Nothing happened. I didn't get a messagebox, nothing.

    So how can I get the program to only process the information if the data was actually dropped onto the form, and not if it was dragged onto then out of the form?
    Did you set the Form's Property AllowDrop?

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Originally posted by HaLo2FrEeEk
      I've got a program that requires that the user pick a file or folder on their computer. Currently the user clicks Open Single in the File menu and an openFileDialog comes up; to open a folder the user clicks Batch from the File menu and an folderBrowserDi alog comes up. I'd like to expand this to allow the user to simply drag a file or folder onto the program window, and I'm realizing how complicated drag n drop can be.

      I'm starting by just testing folders. I've set up an event handler for the dragEnter and dragLeave events. When I drag a folder over the form the dragOver event handler confirms that the data being dragged is actually a folder and returns either true or false. The dragLeave handler then displays all the files in that folder when I let go of the mouse. The problem here is that while letting go of the mouse completes the drag and drop, so does dragging my mouse outside the form's bounds. I need a way to confirm that the data was actually dropped onto the form.

      I've tried the DragDrop event, but it doesn't seem to do anything. I tried this simple code:

      Code:
      private void Form1_DragDrop(object sender, DragEventArgs e)
      {
          MessageBox.Show("Data was dropped");
      }
      then I tried dropping something onto the form. Nothing happened. I didn't get a messagebox, nothing.

      So how can I get the program to only process the information if the data was actually dropped onto the form, and not if it was dragged onto then out of the form?
      I actually wrote an article here on this site about this:

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        Ergh, I actually ended up figuring it out, I would like to take a look at that link though, thanks!

        Comment

        Working...