2016-05-18 129 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; 
    } 
}