Sampling drag-and-drop between WinForm RichTextBoxes within one application and between them and external applications revealed two interesting observations (item 1 certainly appears to be a bug; in a perfect world, item 2 would probably be as well):
- Some drag-and-drop operations delete the dragged text from the source container, whether or not it is set to read-only. (Thanks to Mark Morgan for first noticing this in his bug report on my open-source site.)
- Whether text is retained or deleted from a source container is inconsistent among different applications.
I could not find any definitive reference indicating what drag-and-drop behavior is supposed to be. The closest I found was on page 476 of the Windows User Experience Interaction Guidelines (for Vista): "Dragging and dropping: Object is moved or copied to the drop target". Well, that certainly aligns with my observations; some applications move the object, others copy it!
The questions: I would like to find a workaround for item 1 above; it irks me that a read-only container is not inviolate! As a secondary question, I am wondering if someone has a reference to how drag-and-drop is supposed to behave? When is it a move and when is it a copy?
My sample WinForm application (code below) contains two RichTextBox controls, the left one being read-only (call this RTB1) and initialized with some text; the right one (RTB2) being read/write so it may receive text. Both have drag-and-drop enabled for the test. Here are the combinations I tested; notice that in each grouping there is at least one "odd-man-out" :
- From RTB1 to RTB2: move
- From RTB1 to other RTB (external): move
- From RTB1 to WordPad: copy
- From RTB1 to Word2003: move
- From RTB1 to Outlook2003: copy
From RTB1 to Firefox3.0: copy
From RTB2 to other RTB (external): move
- From RTB2 to WordPad: copy
- From RTB2 to Outlook2003: copy
From RTB2 to Firefox3.0: copy
From Outlook2003 to RTB2: move
- From WordPad to RTB2: move
- From Word2003 to RTB2: move
- From other RTB (external) to RTB2: move
From Firefox3.0 to RTB2: copy
From Word2003 to Outlook2003: copy
- From Outlook2003 to Word2003 : move
Tests run on WinXP.
Test app compiled with .NET 2.0 (tried a couple with .NET 3.5 with the same results).
Here is the sample application:
using System;
using System.Windows.Forms;
namespace RichTextBoxTest
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
partial class Form1 : Form
{
private RichTextBox richTextBox1 = new RichTextBox();
private RichTextBox richTextBox2 = new RichTextBox();
public Form1()
{
InitializeComponent();
}
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.EnableAutoDragDrop = true;
this.richTextBox1.Location = new System.Drawing.Point(34, 25);
this.richTextBox1.ReadOnly = true;
this.richTextBox1.Size = new System.Drawing.Size(122, 73);
this.richTextBox1.Text = "some stuff here";
//
// richTextBox2
//
this.richTextBox2.EnableAutoDragDrop = true;
this.richTextBox2.Location = new System.Drawing.Point(177, 25);
this.richTextBox2.Size = new System.Drawing.Size(122, 73);
this.richTextBox2.Text = "";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(338, 122);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.richTextBox1);
this.Text = "Form1";
this.ResumeLayout(false);
}
}
}