2015-12-04 84 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 。有多种方法可以解决这个问题,您需要考虑让用户纠正错误的方式。