Drag and Drop from one form to a 2nd form

G

Guest

I've been trying to perform a drag and drop operation from a PictureBox
control in a standard C# form (with a FixedToolWindow border) to another
standard C# form, but cannot get it to work properly.

In the FixedToolWindow, I have the following code:
myClass c = new myClass(); // contains public variable m_MouseIsDown

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (! (pictureBox1.Image == null))
c.m_MouseIsDown = true;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (c.m_MouseIsDown)
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
}

In the second form, I have the DropAllow property enabled and have the
following code in it:
myClass c = new myClass(); // contains public variable m_MouseIsDown
private int picboxCount;
private PictureBox picbox;

private void Form1_DragDrop(object sender, DragEventArgs e)
{
if (c.m_MouseIsDown)
{
int i = picboxCount;
picbox = new PictureBox();

this.Controls.Add(picbox);

picbox.Name = "pictureBox" + i.ToString();
picbox.BorderStyle = BorderStyle.FixedSingle;
picbox.Size = new Size(24, 24);
picbox.Image = (Image)e.Data.GetDataPresent(DataFormats.Bitmap);
picbox.Location = this.PointToClient(new Point(e.X - 12, e.Y -
12));

picboxCount++;
c.m_MouseIsDown = false;
}
}

private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}

One problem I've encountered is in the statement
picbox.Image = (Image)e.Data.GetDataPresent(DataFormats.Bitmap);
as I get a compilation error stating that it cannot convert from type 'bool'
to 'System.Drawing.Image'. Commenting it out and hardcoding a value for the
image, I still can't get the drag and drop operation to work.

Any help would be greatly appreciated!

Allen
 
G

Guest

I said:
One problem I've encountered is in the statement
picbox.Image = (Image)e.Data.GetDataPresent(DataFormats.Bitmap);
as I get a compilation error stating that it cannot convert from type 'bool'
to 'System.Drawing.Image'. Commenting it out and hardcoding a value for the
image, I still can't get the drag and drop operation to work.
Replacing that line with one reading
picbox.Image = System.Drawing.Image.FromFile("C:\\concatenate.bmp");
allows the project to be compiled and I am able to successfully perform the
drag and drop operation.

Any ideas on how I can use the bitmap from the control being "dragged" and
placed into the newly-created PictureBox on the primary form? Thanks!
 
G

Guest

GetDataPresent does not return the dragged data. Use GetData instead for
this. Try the following code:

if (e.Data.GetDataPresent(DataFormats.Bitma))
picbox.Image = (Image) e.Data.GetData(DataFormats.Bitmap);

HTH, Jakob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top