2014-11-20 2 views
1

Я следующий код, в котором я пытаюсь реализовать функцию перетаскивания:DragDrop и DragEnter стрельбы дважды

public Location_Alert() 
    { 
     InitializeComponent(); 
     #region Form Initialization 
     Recur_Txt.Text = "1"; 
     End_Date.Value = Start_Date.Value.Add(new TimeSpan(1,0,0,0)); 
     Recur_Time_Txt.Text = DateTime.Now.Add(new TimeSpan(0,15,0)).ToString("HH:mm"); 
     Location_Alert_Timer.Tick += new EventHandler(Location_Alert_Timer_Tick);//allow for timed recurrences in code 
     this.DragEnter += new DragEventHandler(Location_Alert_DragEnter);//set up monitoring for a Drag event, changing the cursor as users drags file(s) onto the form. 
     this.DragDrop +=new DragEventHandler(Location_Alert_DragDrop);//set up monitoring for a Drop event, allowing user to drag file onto the form 
     #endregion 
    } 

    private void Location_Alert_DragDrop(object sender, DragEventArgs e) 
    { 
     string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); 
     if (files.Length == 1) 
     { 
      int i = 0; 
      int ii = -1; 
      foreach(string file in files) 
      { 
       if (file.Contains("enot")) 
       { 
        if (ii == -1) 
        { 
         ii = i; 
        } 
       } 
       i++; 
      } 
      ImportFile(files[ii]); 
     } 
     else 
     { 
      MessageBox.Show("This application only supports a single drag and drop file, only the first *.enot file will be imported."); 
      int i = 0; 
      int ii = -1; 
      foreach (string file in files) 
      { 
       if (file.Contains("enot")) 
       { 
        if (ii == -1) 
        { 
         ii = i; 
        } 
       } 
       i++; 
      } 
      ImportFile(files[ii]); 
     } 
    } 

    private void Location_Alert_DragEnter(object sender, DragEventArgs e) 
    { 
     if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
     { 
      int i = 0; 
      int ii = -1; 
      string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); 
      foreach (string file in files) 
      { 
       if (file.Contains("enot")) 
       { 
        if (ii == -1) 
        { 
         ii = i; 
        } 
       } 
       i++; 
      } 
      if (ii != -1) 
      { 
       e.Effect = DragDropEffects.Copy; 
      } 
      else 
      { 
       e.Effect = DragDropEffects.None; 
      } 
     } 
    } 

и DragDrop и события DragEnter всегда срабатывать дважды. Мои тиковые события не делают, я не уверен относительно причины этого. Дополнительные пожары не вызывают проблемы, кроме случаев, когда пользователь попадает в исключение. это будет обрабатывать исключение дважды, что в этом случае означает 2 сообщения для пользователя.

+3

Возможны ли у вас события, связанные дважды? Один раз от дизайнера, один раз в конструкторе? Попробуйте прокомментировать конструкторы и посмотрите, все ли работает. – LarsTech

ответ

1

@LarsTech получил ответ: конструкторы событий были объявлены в моем представлении дизайнера, а также в моем конструкторе форм.

1

Даже если вызов dragEnter был вызван дважды, dragDrop всегда вызывается один раз, если вы положили e.Effect = DragDropEffects.Copy в dragEnter.

private void Location_Alert_DragDrop(object sender, DragEventArgs e) 
{ 
    ToDo(); 
} 
private void Location_Alert_DragEnter(object sender, DragEventArgs e) 
{ 
    e.Effect = DragDropEffects.Copy; 
} 
Смежные вопросы