I set to true
the AllowDrop
implemented the DragOver
and DragDrop
events RichTextBox. On DragDrop
event I load the dropped text files' contents on the RTB but it does add the icon of the file in RTB I'd to remove it:
Edit: Here's my code:
void msg_setup_dragDrop()
{
msg_textBox.AllowDrop = true;
msg_textBox.EnableAutoDragDrop = true; msg_textBox.DragEnter += new DragEventHandler(msg_DragEnter); msg_textBox.DragDrop += new DragEventHandler(msg_DragDrop); }
void msg_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
void msg_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
StringBuilder buffer = new StringBuilder();
foreach (string filename in files)
{
try
{
string text = File.ReadAllText(filename);
buffer.Append(text);
}
catch (Exception ex)
{
string errMsg = string.Format("cannot read the file\"{0}\" error: {1}", filename, ex.Message);
MessageBox.Show(errMsg, "Reading file error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
msg_textBox.Text = buffer.ToString();
}
Somewhere you have set msg_textBox.EnableAutoDragDrop = true
, either in your designer window or your code. You need to set this to false. You do still need to set AllowDrop = true
.
When set to true, the winforms RichTextBox
provides standard behaviors for drag-and-drop events, to which your custom handlers are added. If you don't want the standard behavior, you have to completely roll your own handlers. (The standard behavior for a dropped text file is OLE embedding. If you double click on the icon, notepad launches.)