简体   繁体   English

DragDrop和DragEnter触发两次

[英]DragDrop and DragEnter firing twice

I have the following code in which I am attempting to implement a drag and drop functionality: 我有以下代码,我试图实现拖放功能:

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;
            }
        }
    }

and the dragDrop and DragEnter events always fire twice. 并且dragDrop和DragEnter事件总是触发两次。 My tick events do not, I am unsure as to the reason behind this. 我的嘀嗒事件没有,我不确定这背后的原因。 The extra fires are not causing an issue except if the user hits an exception. 除非用户遇到异常,否则额外的火灾不会引起问题。 this will handle the exception twice, which in this case means 2 messageboxes back to the user. 这将处理异常两次,在这种情况下意味着2个消息框返回给用户。

@LarsTech得到了答案:事件构造函数在我的设计器视图和表单构造函数中声明。

Even if the dragEnter has been called twice, the dragDrop is always called once, if you put e.Effect = DragDropEffects.Copy in dragEnter. 即使已经调用了两次dragEnter,如果在dragEnter中放置e.Effect = DragDropEffects.Copy,则总是调用dragDrop一次。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM