the above code pasted can only drag the button but not into the picturebox. pls help.

hi, 
i need to drag a button into a picturebox and when the button is dragged inside the picturebox, i want it to be able to move around also. 

but my code i did only allow me to drag the button but not into the picturebox. 




my code: 

  private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
           

            Panel source = (Panel)sender;
            DoDragDrop(source.BackgroundImage, DragDropEffects.Copy);

           
        }

  private void pictureBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(Bitmap)))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }

        }

private void pictureBox1_DragDrop(object sender, DragEventArgs e)
        {
            
            Panel destination = (Panel)sender;
            destination.BackgroundImage = (Bitmap)e.Data.GetData(typeof(Bitmap));
} 






need help. thank you (:

Recommended Answers

>drag button into picturebox.

You can drag/drop the data from source control to the destination control.

Take a look at this article - http://msdn.microsoft.com/en-us/library/aa289508%28VS.71%29.aspx

Jump to Post

You can't drop a button in a picturebox. Read my post #3 - Drag & drop allows you to move data. In fact you are trying to move button control from one point of form to another point of form.

Jump to Post

All 6 Replies

HEY, anyone help ?

sorry, had tried all the ones u given. but it does not allow me to drop it into a picturebox and continue to drag it further inside the picturebox.

You can't drop a button in a picturebox. Read my post #3 - Drag & drop allows you to move data. In fact you are trying to move button control from one point of form to another point of form.

If you want to move the button around, use the mousedown and mouse up events to capture the start and end point of the mouse drag and alter the buttons location property according to the direction and distance the mouse moves

Control last;
Point lastp;

private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
     ((Control)sender).Cursor = System.Windows.Forms.Cursors.NoMove2D;
}

private void textBox1_MouseUp(object sender, MouseEventArgs e)
{

            last = (Control)sender;
            lastp = new Point(last.Location.X + e.Location.X, last.Location.Y + e.Location.Y);
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
            Panel p = ((Panel)sender);
            bool viz = (lastp.X >= p.Location.X + e.Location.X - 1) && (lastp.X <= p.Location.X + e.Location.X + 1);
            bool fug = (lastp.Y >= p.Location.Y + e.Location.Y - 1) && (lastp.Y <= p.Location.Y + e.Location.Y + 1);
            if (last != null && lastp != null && viz && fug )
            { 
                p.Controls.Add(last);
                last.Location = new Point(30, 30);

                p.Refresh();
            }  
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.