@climbage 提供的答案有很好的解释。这里是如何在 RichTextBox 中实现拖放
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace rich_RichtextboxDragDrop
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
AllowDrop = true;
this.richTextBox1.DragEnter += new DragEventHandler(richTextBox1_DragEnter);
this.richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
}
void richTextBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if ((e.Data.GetDataPresent(DataFormats.FileDrop)))
{
e.Effect = DragDropEffects.Copy;
}
}
void richTextBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
Image img = default(Image);
img = Image.FromFile(((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString());
Clipboard.SetImage(img);
this.richTextBox1.SelectionStart = 0;
this.richTextBox1.Paste();
}
}
}
编辑以获取更多信息
这就是您在“属性”选项卡中看不到它的原因。Attribute [Browsable(false)]
告诉PropertyGrid
不要显示该属性。这是来自 MSDN 的源代码。
/// <include file='doc\RichTextBox.uex' path='docs/doc[@for="RichTextBox.DragEnter"]/*' />
/// <devdoc>
/// RichTextBox controls have built-in drag and drop support, but AllowDrop, DragEnter, DragDrop
/// may still be used: this should be hidden in the property grid, but not in code
/// </devdoc>
[Browsable(false)]
public new event DragEventHandler DragEnter {
add {
base.DragEnter += value;
}
remove {
base.DragEnter -= value;
}
}