Drag and drop in read only rich text box

喜你入骨 提交于 2019-12-23 15:41:14

问题


I am trying implement drag and drop feature in RichTextBox (windows common control). It works fine using the code shown below. However, the drag drop event is not getting triggered when I set the ReadOnly property to true. Is there anything that I am missing? or is that the right behaviour ? Please advice.

private void rtb_dragdrop(object sender, DragEventArgs e)
{
    Console.WriteLine("Test");       
}

private void rtb_dragenter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Copy;
}

回答1:


Obviously, setting the ReadOnly property to true in a RichTextBox will turn off the ability to do Drag and Drop operations.

A simple hack to mimic a read only RichTextBox:

public partial class Form1 : Form {

  public Form1() {
    InitializeComponent();
    rtb.EnableAutoDragDrop = true;
    rtb.KeyDown += new KeyEventHandler(rtb_KeyDown);
  }

  void rtb_KeyDown(object sender, KeyEventArgs e) {
    e.SuppressKeyPress = true;
  }
}

Now your Drag and Drop operation should work automatically with the EnabledAutoDragDrop property set to true. No need to handle those drag enter and drop events.



来源:https://stackoverflow.com/questions/11491988/drag-and-drop-in-read-only-rich-text-box

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!