2016-05-18 100 views
-1

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

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)); 
    } 
+0

请添加代码 –

+0

使用一个标志,可能在PB的标签中允许/禁止拖动!在'pictureBox1_MouseDown'测试它在每个'_DragDrop'事件中设置它! – TaW

+0

@TaW我的想法确切:-) –

回答

1

相反拖动PictureBox的图像,拖动PictureBox
删除时,将它的标签属性设置为true

MouseDown事件中,检查Tag属性是否为空,并且仅在它为时拖动。

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; 
    } 
}