gpt4 book ai didi

c# - DragDrop 和 DragEnter 触发两次

转载 作者:行者123 更新时间:2023-11-30 14:29:10 24 4
gpt4 key购买 nike

我有以下代码,我试图在其中实现拖放功能:

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 个消息框。

最佳答案

@LarsTech 给出了答案:事件构造函数是在我的设计器 View 和表单构造函数中声明的。

关于c# - DragDrop 和 DragEnter 触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27050365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com