views:

361

answers:

1

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):

  1. 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.)
  2. 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" :


  1. From RTB1 to RTB2: move
  2. From RTB1 to other RTB (external): move
  3. From RTB1 to WordPad: copy
  4. From RTB1 to Word2003: move
  5. From RTB1 to Outlook2003: copy
  6. From RTB1 to Firefox3.0: copy


  7. From RTB2 to other RTB (external): move

  8. From RTB2 to WordPad: copy
  9. From RTB2 to Outlook2003: copy
  10. From RTB2 to Firefox3.0: copy


  11. From Outlook2003 to RTB2: move

  12. From WordPad to RTB2: move
  13. From Word2003 to RTB2: move
  14. From other RTB (external) to RTB2: move
  15. From Firefox3.0 to RTB2: copy


  16. From Word2003 to Outlook2003: copy

  17. 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);
        }
    }
}
+1  A: 

Having had no tidbits submitted on this I delved further into the issue.

First, I obtained some information from Microsoft (via MSDN support) that standard drag-and-drop behavior does a move while holding down Control with drag-and-drop does a copy.

Next, consider these three modes of operation:

  1. User can edit text.
  2. User can move text (via drag-and-drop).
  3. Application can change text programmatically.

According to Microsoft, setting read-only disables only item (1) ! To also honor read-only for item (2) one must manually code the solution rather than using the read-only property.

Well, to me this is clearly a defect. I believe read-only should disable both (1) and (2). So I submitted an official defect report to Microsoft Connect espousing this opinion. Alas, the response came back as essentially "Thanks, but not important enough issue to fix." Sigh...

msorens
Thanks for the info; useful stuff.
Joel in Gö