C# Richtextbox拖放与表单拖放不同
因此,我有一个richtextbox和一个表单,并为它们附加了一个拖放事件。它们的代码完全相同,我已经确定了,但是由于某种原因,richtextbox事件激活了文本更改事件。表单也会激活文本更改事件,但我在方法中有一个变量,我从拖动事件中调用该变量来阻止它。我尝试在不同的表单上复制它,但主要的问题是表单拖放事件的代码与richtextbox拖放事件完全相同。他们怎么可能不同呢?以下是投递代码:C# Richtextbox拖放与表单拖放不同,c#,.net,winforms,drag-and-drop,richtextbox,C#,.net,Winforms,Drag And Drop,Richtextbox,因此,我有一个richtextbox和一个表单,并为它们附加了一个拖放事件。它们的代码完全相同,我已经确定了,但是由于某种原因,richtextbox事件激活了文本更改事件。表单也会激活文本更改事件,但我在方法中有一个变量,我从拖动事件中调用该变量来阻止它。我尝试在不同的表单上复制它,但主要的问题是表单拖放事件的代码与richtextbox拖放事件完全相同。他们怎么可能不同呢?以下是投递代码: private void TestBox_DragEnter(object sender, D
private void TestBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private async void TestBox_DragDrop(object sender, DragEventArgs e)
{
string filePath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
await AsyncStuff();
}
private async Task AsyncStuff()
{
block = true;
await OtherAsyncMethodThatActivatesTextChanged();
block = false;
}
private bool block;
private void TestBox_TextChanged(object sender, EventArgs e)
{
if (block) return;
MessageBox.Show("Text Changed called"); //Activates on the richtextbox drag, doesn't on the form
}
我现在可以复制了,这是代码。我附加了表单拖放,并通过设计器输入事件
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
richTextBox1.AllowDrop = true;
richTextBox1.TextChanged += TestBox_TextChanged;
richTextBox1.DragDrop += TestBox_DragDrop;
richTextBox1.DragEnter += TestBox_DragEnter;
AllowDrop = true;
}
private void TestBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private async void TestBox_DragDrop(object sender, DragEventArgs e)
{
string filePath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
await AsyncStuff();
}
private async Task AsyncStuff()
{
block = true;
richTextBox1.AppendText("asd");
block = false;
}
private bool block;
private void TestBox_TextChanged(object sender, EventArgs e)
{
if (block) return;
MessageBox.Show("Text Changed called"); //Activates on the richtextbox drag, doesn't on the form
}
private async void Form1_DragDrop(object sender, DragEventArgs e)
{
string filePath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
await AsyncStuff();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
}