C#拖放图片框2

时间:2016-05-18 07:47:08

标签: c# winforms

我有问题。我有一组图片框被拖入一个图片框。如何在拖动后禁用特定的图片框?所以,它不能再拖了。

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  {
    if (e.Button == MouseButtons.Left)
      pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.All);
  }

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

  private void pictureBox2_DragDrop(object sender, DragEventArgs e)
  {
    if ((e.Data.GetDataPresent(DataFormats.Bitmap)))
      this.pictureBox2.Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
  }

1 个答案:

答案 0 :(得分:1)

不要拖动PictureBox的图片,而是拖动PictureBox 删除后,将其标记属性设置为true

MouseDown事件中,检查Tag属性是否为null,并且只有在它被拖动时才拖动。

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && pictureBox1.Tag == null)
        pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.All);
}

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

private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(PictureBox)))
    {
        var picturebox = ((PictureBox)e.Data.GetData(typeof(PictureBox)));
        picturebox.Tag = true;
        this.pictureBox2.Image = picturebox.Image;
    }
}