locked
Drag_enter executes twice RRS feed

  • Question

  • Hello,
    Im writing a program and i have a listbox that i can drag some files in it and add their filenames.
    It goes something like this:

            private void PatchList_DragEnter(object sender, DragEventArgs e)
    {
    string[] filedroped = e.Data.GetData(DataFormats.FileDrop) as string[];
               for (i = 0; i <= filedroped.Length - 1; i++)
                    {
    some code here checking file extensions
    }

    .............
    and below if the extensions are correct add the filenames ti the list else and moving files to another folder

    Code runs well i have no problems even it'smy first time im working with dragging events but i have this one problem
     this whole code  private void PatchList_DragEnter(object sender, DragEventArgs e) executes twice. And i don't know why.
    Any help?

    Tuesday, May 20, 2008 10:24 PM

Answers

  • Well, but now it doesn't matter any more.  Getting two DROPs would be a real problem.
    Monday, May 26, 2008 7:19 PM

All replies

  • Even with this code the routine executes twice:

     private void listbox_DragEnter(object sender, DragEventArgs e)
            {
                return;
            }

    The DragEnter is called twice. How this is happening?
    Wednesday, May 21, 2008 8:47 AM
  •  

    Hi de_Stan,

    Sorry, I cannot reproduce your problem. Please check whether you have registered this event twice or not. Could you please provide more detailed steps to reproduce this problem?

    Thanks!
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Monday, May 26, 2008 11:06 AM
  • Hello Rong thank you for the reply.

    How should i check if i have registered the event twice?

     

    This is what i have. I created a List Box and i want to drag files in it and add thei filenames if the extensions are right. To be honest i dont use the Drag_Drop or Drag_Over events as i saw at th MSDN examples. Just the Drag_Enter event and i added the code in it. My code runs fine but somehow it runs twice. At first i thought maybe it was something in the code and i deleted it and just wrote "return;" but the same was happening. I aslo put a Messagebox

     

    private void listbox_DragEnter(object sender, DragEventArgs e)
            {
                MessageBox.Show("TEST");
            }

    and it showed up twice meaning this routine is called from somewhere a second time.

     

    I also found out this. If i move the mouse (while dragging) fast enough that it wll pass the program's window

    and the listbox and don't stay in it the code will execute ONCE. If i do normal dragging and the mouse stay in the listbox it runs twice.So i guess it has to do something with that?
    Monday, May 26, 2008 11:55 AM
  •  de_Stan wrote:

    Hello Rong thank you for the reply.

    How should i check if i have registered the event twice?

     

    This is what i have. I created a List Box and i want to drag files in it and add thei filenames if the extensions are right. To be honest i dont use the Drag_Drop or Drag_Over events as i saw at th MSDN examples. Just the Drag_Enter event and i added the code in it. My code runs fine but somehow it runs twice. At first i thought maybe it was something in the code and i deleted it and just wrote "return;" but the same was happening. I aslo put a Messagebox

     

    private void listbox_DragEnter(object sender, DragEventArgs e)
            {
                MessageBox.Show("TEST");
            }

    and it showed up twice meaning this routine is called from somewhere a second time.

     

    I also found out this. If i move the mouse (while dragging) fast enough that it wll pass the program's window

    and the listbox and don't stay in it the code will execute ONCE. If i do normal dragging and the mouse stay in the listbox it runs twice.So i guess it has to do something with that?

     

     

     

    You may search "listbox_DragEnter" in the entire solution....

     

    If you find it more than once please delete that line, and dont count the line which is used for method declaration.

     

    Chintankumar Patel

    http://www.nineon.com

     

    Monday, May 26, 2008 12:13 PM
  • I did that and only found these that they are recuired:

    1) private void listbox_DragEnter(object sender, DragEventArgs e) in the form
    2) this.listbox.DragOver += new System.Windows.Forms.DragEventHandler(this.listbox_DragEnter); in the designer.cs
    Monday, May 26, 2008 2:42 PM
  • I can't repro either.  Maybe the app that started the D+D is temporarily stealing focus, who knows.  Stay out of trouble and implement D+D like it is supposed to be implemented:

        private void listBox1_DragEnter(object sender, DragEventArgs e) {
          if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
        }
        private void listBox1_DragDrop(object sender, DragEventArgs e) {
          string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
          foreach (string file in files)
            listBox1.Items.Add(file);
        }
    Monday, May 26, 2008 4:25 PM
  • I did as you nobugz said but still i have the same problem. I created a new solution and have this code:

    private void listBox1_DragEnter(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                    e.Effect = DragDropEffects.Copy;
                MessageBox.Show("ENTER");
            }

            private void listBox1_DragDrop(object sender, DragEventArgs e)
            {
                MessageBox.Show("DROP");
            }

    Running this code i get twice the "ENTER" message (so it really executes twice somehow) and one time the DROP message (this is ok).

    I can't understand it.
    Monday, May 26, 2008 7:07 PM
  • Well, but now it doesn't matter any more.  Getting two DROPs would be a real problem.
    Monday, May 26, 2008 7:19 PM