Drop event on a picture box

J

Jeff Williams

I have several picture boxes on a form. I want to be able to drag
pictures to this picture box. On the Drop how can I tell which picture
box I am dropping the picture on. Te picture Box control name or
control text would suit my needs.

Regards
Jeff
 
A

Alberto Poblacion

Jeff Williams said:
I have several picture boxes on a form. I want to be able to drag pictures
to this picture box. On the Drop how can I tell which picture box I am
dropping the picture on. Te picture Box control name or control text would
suit my needs.

Are you using a single DragDrop event for all your pictureboxes? In that
case, the first argument, "sender", is a reference to the control that
triggered the event, which is the picturebox in which you are dropping the
picture. Since it is of type Object, you will have to cast it into
PictureBox.

private void MyPictureBoxes_DragDrop(object sender, DragEventArgs e)
{
PictureBox dropTarget = sender as PictureBox;
if (dropTarget==null) return; //Should never happen
if (e.Data.GetDataPresent(typeof(Image))) {
Image item = (Image)e.Data.GetData(typeof(Image));
if (e.Effect == DragDropEffects.Copy ||
e.Effect == DragDropEffects.Move) {
dropTarget.Image = item;
}
}
}
 

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