Hi there,
I have 2 richTextBoxes and I want them to be able to drag and drop between
the 2 of them. Below doesn't work because each DragDrop gets executed when it
is at the source OR target. (e.g. When you drag from richTextBox1 into
richTextBox2 richTextBox1_DragDrop is executed and when you drop into
richTextBox1 from richTextBox2 richTextBox1_DragDrop is executed, too!)
private void richTextBox1_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void richTextBox1_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
// Get start position to drop the text.
int i = richTextBox1.SelectionStart;
string s = richTextBox1.Text.Substring(0, i);
string t = richTextBox1.Text.Substring(i);
// Drop the text on to the RichTextBox.
richTextBox1.Text = s +
e.Data.GetData(DataFormats.Text).ToString() + t;
}
private void richTextBox2_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void richTextBox2_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
int i;
String s;
// Get start position to drop the text.
i = richTextBox2.SelectionStart;
s = richTextBox2.Text.Substring(i);
richTextBox2.Text = richTextBox2.Text.Substring(0, i);
// Drop the text on to the RichTextBox.
richTextBox2.Text = richTextBox2.Text +
e.Data.GetData(DataFormats.Text).ToString() + s;
}
How do you know which way it is heading (Drag or Drop)?
Scotty