2015-12-04 125 views
0

我目前正試圖簡化我們在工作中使用的工具。爲此,我想使用拖放方法。這個工具基本上就像使用三種不同類型的建築物一樣。在頂部有下面三個不同塊的圖像是一個流佈局面板。目標是將所需塊順序拖放到流佈局面板中。vb.net使用圖像拖放操作

這是一張代表起始位置的快速圖像。 (只是要清楚。) enter image description here

這對我來說是棘手的部分。我只使用拖放方法將一個文本框中的值刪除到另一個文本框中。現在我需要複製圖像對象並將其添加到流佈局面板中。

這是我跟着拖動方法,爲下一步我應該做的圖像一樣,這裏降值

Private MouseIsDown As Boolean = False 

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As _ 
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown 
    ' Set a flag to show that the mouse is down. 
    MouseIsDown = True 
End Sub 

Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As _ 
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove 
    If MouseIsDown Then 
     ' Initiate dragging. 
     TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy) 
    End If 
    MouseIsDown = False 
End Sub 

Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As _ 
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter 
    ' Check the format of the data being dropped. 
    If (e.Data.GetDataPresent(DataFormats.Text)) Then 
     ' Display the copy cursor. 
     e.Effect = DragDropEffects.Copy 
    Else 
     ' Display the no-drop cursor. 
     e.Effect = DragDropEffects.None 
    End If 
End Sub 

Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As _ 
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop 
    ' Paste the text. 
    TextBox2.Text = e.Data.GetData(DataFormats.Text) 
End Sub 

現在是我的嘗試:

Public Class Form2 

    Private MouseIsDown As Boolean = False 

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ 
     Handles PictureBox1.MouseDown 
     ' Set a flag to show that the mouse is down. 
     MouseIsDown = True 
    End Sub 

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As _ 
            System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove 
     If MouseIsDown Then 
      ' Initiate dragging. 
      PictureBox1.DoDragDrop(PictureBox1, DragDropEffects.Copy) 
     End If 
     MouseIsDown = False 
    End Sub 

    Private Sub FlowLayoutPanel1_DragEnter(ByVal sender As Object, ByVal e As _ 
              System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragEnter 
     ' Check the format of the data being dropped. 
     If (e.Data.GetDataPresent(DataFormats.Text)) Then 
      ' Display the copy cursor. 
      e.Effect = DragDropEffects.Copy 
     Else 
      ' Display the no-drop cursor. 
      e.Effect = DragDropEffects.None 
     End If 
    End Sub 

    Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As _ 
System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop 
     ' Paste the text. 
     FlowLayoutPanel1.Text = e.Data.GetData(DataFormats.Bitmap) 
    End Sub 

End Class 

但如果我這樣做,並將picturebox1項目拖到面板上,我只得到不能放下符號。所以這就是我被卡住的地方。有人可以給我提供一些信息如何做到這一點?或者給我一些指點?

+0

您的'DoDragDrop'以'Control'作爲要拖放的數據(與問題標題匹配)啓動,但您在dragenter中測試'DataFormats.Text'。 – Plutonix

回答

2
' Check the format of the data being dropped. 
    If (e.Data.GetDataPresent(DataFormats.Text)) Then 

這是不正確的。你現在正在拖動一個PictureBox對象,它不是文本,所以如果表達式總是會是False。當你看到一個被拖動的PictureBox對象時,你纔開心。就像這樣:

If (e.Data.GetDataPresent(GetType(PictureBox))) Then 
在DragDrop事件處理程序

同樣的問題,它需要像:

Dim pb = CType(e.Data.GetData(GetType(PictureBox))) 
    FlowLayoutPanel1.Controls.Add(pb) 

,或者您想要創建一個新的,指定圖像屬性,設置pb.Image爲Nothing 。有多種方法可以解決這個問題,您需要考慮讓用戶糾正錯誤的方式。