Reputation: 342
I have a simple page. One Rich Text Box bound to a test table in my database.
I have turned the EnableAutoDragDrop to true.
Everything works great, the contents of the box are saved and able to pull back up when asked for.
My issue is dropping image into the RTB. If I drag them directly from the file manager (any type of image file) then I get an Icon with the file name that shows up, not the actual image.
If I open up Word and drop the image into Word, then drag it into the RTB then the image shows just fine.
I guess I dont understand the mechanics of the process between the file manager and word and my RTB. Can anyone elighten me?
Upvotes: 0
Views: 2685
Reputation: 3721
Answer provided by @climbage has good explaination. here is how to achieve drag and drop in RichTextBox
Code:
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();
}
}
}
Edited for more information
This is why you don't see it in the PropertiesTab. The Attribute [Browsable(false)]
tells the PropertyGrid
not to display the property. Here's source the code from 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;
}
}
Upvotes: 4
Reputation: 10941
The drag event can contain multiple format types that are determined by the source of the drag event. When I drag an image (.png) from a the file system to a C# control, I get this set of available formats (note you can get these from DragEventArgs.Data.GetFormats()
)
Shell IDList Array
Shell Object Offsets
DragImageBits
DragContext
InShellDragLoop
FileDrop
FileNameW
FileName
Now when I drag that same image on to word, and then to my C# control, I get this list of formats :
Woozle
Object Descriptor
Rich Text Format
HTML Format
System.String
UnicodeText
Text
EnhancedMetafile
MetaFilePict
Embed Source
It is entirely up to the target control to determine how to handle the drag data, and which format(s) to use. MS Word may take the format FileNameW
, which is just the path to the file dropped, and read the image. While the RichTextBox
probably takes the FileNameW
and gets its icon.
Upvotes: 1