Drop event on a picture box

  • Thread starter Thread starter Jeff Williams
  • Start date Start date
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
 
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;
}
}
}
 
Back
Top