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);
        }
    }
}
4

1 回答 1

1

由于没有就此提交花絮,我进一步深入研究了这个问题。

首先,我从 Microsoft(通过 MSDN 支持)获得了一些信息,即标准的拖放行为会移动,同时按住 Control 并拖放会进行复制

接下来,考虑这三种操作模式:

  1. 用户可以编辑文本。
  2. 用户可以移动文本(通过拖放)。
  3. 应用程序可以以编程方式更改文本。

根据微软的说法,设置只读只会禁用第 (1) 项!为了也尊重第 (2) 项的只读,必须手动编码解决方案,而不是使用只读属性。

好吧,对我来说,这显然是一个缺陷。我相信只读应该禁用(1)和(2)。因此,我向Microsoft Connect提交了一份官方缺陷报告,支持这一观点。唉,回复基本上是“谢谢,但问题不够重要,无法解决。” 叹...

于 2009-08-26T14:38:51.020 回答