Windows Develop Bookmark and Share   
 index > Windows Forms General > DragDrop file into RichTextBox *without* icon?
 

DragDrop file into RichTextBox *without* icon?

I wish to drag a file from a folder on the desktop and drop it into a RichTextBox in my app. It works, but all I really need is the path & filename of the dropped file, which I can get from the dropped event.

However, my RichTextBox always get the big icon of the file (an empty square with the filename to be exact) dropped into the dropped mouse location. How can I prevent this icon from appearing, yet still be able to catch the path/file of the dropped file(s)?
smarr  Thursday, March 22, 2007 4:06 AM
Yes, the behavior is EXACTLY that of Wordpad.
You mentioning this made me think that the problem is in the RichTextBox itself, not the drag and drop code that was already working. A close look through the RTB properties reveals "EnableAutoDragDrop". This was set to true. Apparently this will perform a DD operations independently of your code, and by default displays the aforementioned icon.

Switching EnableAutoDragDrop to be False prevents the automatic attempt at DD and lets you code perform by itself. This is the solution.

Thanks for the assist!
smarr  Friday, March 23, 2007 12:55 AM

        void Form2_DragDrop(object sender, DragEventArgs e)

  label1.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();

void Form2_DragEnter(object sender, DragEventArgs e)

if (e.Data.GetDataPresent(DataFormats.FileDrop))

DragDropEffects.Link;

else e.Effect = DragDropEffects.None;

The designer hides all the D+D properties and events for the RTB. Not sure why, it works fine if I set them in code:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
richTextBox1.AllowDrop = true;
richTextBox1.DragEnter += richTextBox1_DragEnter;
richTextBox1.DragDrop += richTextBox1_DragDrop;
}
private void richTextBox1_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Link;
else e.Effect = DragDropEffects.None;
}
private void richTextBox1_DragDrop(object sender, DragEventArgs e) {
richTextBox1.Text += ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
}
}

nobugz  Thursday, March 22, 2007 7:46 AM
This does not work, as it does not prevent the icon from appearing in the RichTextBox.
smarr  Thursday, March 22, 2007 12:39 PM
Your code (and that of the previous poster) seems to have the exact same effect as mine--I should stress that I have Drag&Drop *working*--the problem is that I want to *prevent* the "icon" from being dropped into my rich text box, yet I *need* drag & drop operating so that I can programmatically get the filename that was dropped. Emphasis on *not getting the icon*.

Perhaps this isn't a drag&drop issues, but a richtextbox issue?
smarr  Thursday, March 22, 2007 12:46 PM
Odd problem. I've seen it before, WordPad for example (one big RTB) does this. Our DragDrop event handlers however explicitly extracts a string from the D+D data object. I don't see how that could produce an icon. Check if this code actually runs or if the RTB is handling the D+D notifications by itself.
nobugz  Thursday, March 22, 2007 3:44 PM
Yes, the behavior is EXACTLY that of Wordpad.
You mentioning this made me think that the problem is in the RichTextBox itself, not the drag and drop code that was already working. A close look through the RTB properties reveals "EnableAutoDragDrop". This was set to true. Apparently this will perform a DD operations independently of your code, and by default displays the aforementioned icon.

Switching EnableAutoDragDrop to be False prevents the automatic attempt at DD and lets you code perform by itself. This is the solution.

Thanks for the assist!
smarr  Friday, March 23, 2007 12:55 AM

You can use google to search for other answers

Custom Search

More Threads

• 'ApplicationDeployment' is not a member of 'Deployment'
• .NET Framework 2.0
• Panel maximum size limitation
• Navigating Forms
• control for a scrolling screen of messages
• ListItem subclassing
• User control like toolbox
• System.ArgumentException when extending ToolStripMenuItem
• Programming DirectShow in Windows Forms
• VB.NET vs AS/400