拖放两侧

时间:2014-05-07 11:56:25

标签: c# drag-and-drop

我想为我的项目制作一个DragAndDrop程序。我想将一张图片从一个图片框拖到另一个图片框,并且工作得非常好。但它不会回到原始图片框。我正在使用6个图片框,第一行有3个,第二行有3个。我已将图片框从第一行拖到第二行,并与正确的描述相结合。是否有可能只制作3个事件(mouse_down,dragenter,dragdrop)和stil每个图片框都可以工作,因为现在我必须为每个图片框制作3个事件,这样我需要18个事件....请帮助meeee

private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox1.AllowDrop = true;
        pictureBox2.AllowDrop = true;
        pictureBox3.AllowDrop = true;
        pictureBox4.AllowDrop = true;
        pictureBox5.AllowDrop = true;
        pictureBox6.AllowDrop = true;

        pictureBox1.MouseDown += MouseDown;
        pictureBox2.MouseDown += MouseDown;


        pictureBox1.DragEnter += pictureBox2_DragEnter;
        pictureBox1.DragDrop += pictureBox2_DragDrop;

        pictureBox2.DragEnter += pictureBox2_DragEnter;
        pictureBox2.DragDrop += pictureBox2_DragDrop;
    }

    /*private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Move);
    }*/

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

    private void pictureBox2_DragDrop(object sender, DragEventArgs e)
    {
        pictureBox2.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
        pictureBox1.Image = null;
    }private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
    {
        pictureBox2.DoDragDrop(pictureBox1.Image, DragDropEffects.Move);
    }

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

    private void pictureBox1_DragDrop(object sender, DragEventArgs e)
    {
        pictureBox1.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
        pictureBox2.Image = null;
    }

    private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
    {
        pictureBox2.DoDragDrop(pictureBox1.Image, DragDropEffects.Move);
    }

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

    private void pictureBox1_DragDrop(object sender, DragEventArgs e)
    {
        pictureBox1.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
        pictureBox2.Image = null;
    }

1 个答案:

答案 0 :(得分:0)

您应该使用在这些事件中获得的sender对象。

我没有将拖动对象设置为图像,而是将其更改为PictureBox本身:

void pictureBox_MouseDown(object sender, MouseEventArgs e) {
  DoDragDrop(sender, DragDropEffects.Move);
}

请注意,这不是pictureBox1_MouseDown,而只是pictureBox_MouseDown。所有PictureBox控件都应将MouseDown事件连接到这一个方法:

pictureBox1.MouseDown += pictureBox_MouseDown;
pictureBox2.MouseDown += pictureBox_MouseDown;
// etc...

您也可以继续其他活动的此过程:

void pictureBox_DragEnter(object sender, DragEventArgs e) {
  e.Effect = DragDropEffects.Move;  
}

对于DragDrop事件,将拖动的对象强制转换回PictureBox控件:

void pictureBox_DragDrop(object sender, DragEventArgs e) {
  PictureBox pb = e.Data.GetData(typeof(PictureBox)) as PictureBox;
  if (pb != null) {
    ((PictureBox)sender).Image = pb.Image;
    pb.Image = null;
  }
}