The example Drag and drop text in C# explains basic drag and drop operations. See that example for the fundamentals. This example explains how to drag and drop a picture.
One of the first things you must do before implementing drag and drop is set the AllowDrop property to true for the control that should accept a drop. For some reason, Visual Studio does not display this property in the properties window for PictureBoxes. IntelliSense doesn't even admit the property exists!
However, the Control class implements AllowDrop and PictureBox inherits from Control so the property does exist. You simply need to set it in code at run time and you need to ignore IntelliSense's refusal to believe it exists. Microsoft says:
The PictureBox control is not designed to allow drag and drop procedures. However, since PictureBox inherits from control, it also inherits the AllowDrop property. To clarify this, in the next release of the documentation, the PictureBox.AllowDrop topic will be marked with the "for internal use only" boilerplate. Also, the PictureBox.AllowDrop property will not appear in intellisense if you are using the Visual Studio text editor.
Basically Microsoft says this is by design and most people wouldn't want to do this anyway. I have no clue what they are thinking.
Anyway the following code shows how the program sets this property at run time.
// Set the drop target PictureBox's AllowDrop property at run time. // It is unavailable in the Properties window and doesn't // even show up in IntelliSense! private void Form1_Load(object sender, EventArgs e) { picDropTarget.AllowDrop = true; }
The following code shows the other drag and drop event handlers used by this example.
// Start the drag. private void picDragSource_MouseDown(object sender, MouseEventArgs e) { // Start the drag if it's the right mouse button. if (e.Button == MouseButtons.Right) { picDragSource.DoDragDrop(picDragSource.Image, DragDropEffects.Copy); } }
// Allow a copy of an image. private void picDropTarget_DragEnter(object sender, DragEventArgs e) { // See if this is a copy and the data includes an image. if (e.Data.GetDataPresent(DataFormats.Bitmap) && (e.AllowedEffect & DragDropEffects.Copy) != 0) { // Allow this. e.Effect = DragDropEffects.Copy; } else { // Don't allow any other drop. e.Effect = DragDropEffects.None; } }
3/21/2011 2:25 PMJuan Carlos wrote:
hey! I've just read and downloaded your example, and it's very good actually, but I'm trying to make a drag-n-drop but I cannot drop the image on the picture box, I've already set the property on, but it stills refuses to open the image, can you give some tips? Reply to this
3/29/2011 7:09 AMRod Stephens wrote:
Well, you need to set the AllowDrop property in code. You also need to catch the DragEnter and DragDrop events for the PictureBox. DragDrop needs to do something with the dragged data--it won't just automatically display the image.
Your best bet may be to take the example and modify or copy it a step at a time until it meets your needs. Reply to this
hey! I've just read and downloaded your example, and it's very good actually, but I'm trying to make a drag-n-drop but I cannot drop the image on the picture box, I've already set the property on, but it stills refuses to open the image, can you give some tips?
Reply to this
Well, you need to set the AllowDrop property in code. You also need to catch the DragEnter and DragDrop events for the PictureBox. DragDrop needs to do something with the dragged data--it won't just automatically display the image.
Your best bet may be to take the example and modify or copy it a step at a time until it meets your needs.
Reply to this