vb.net 使用图片进行拖放操作

vb.net Using the drag and drop operation using images

我目前正在尝试简化我们在工作中使用的工具。为此,我想使用拖放方法。该工具基本上就像使用三种不同类型的块建造一座塔。在顶部有不同块的三个图像,下面是一个流布局面板。目标是以所需的顺序将块拖到流布局面板中。

这是代表起始位置的快速图像。 (只是为了清楚。)

这对我来说是棘手的部分。我只使用拖放方法将值从一个文本框拖放到另一个文本框中。现在我需要复制图像对象并将其添加到流程布局面板中。

这是我拖放值的方法

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 项目拖到面板上,我只会得到不能放下的符号。所以这就是我有点卡住的地方。有人可以向我提供一些信息如何做到这一点吗?或者给我一些指点?

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

这是不正确的。您现在正在拖动一个 PictureBox 对象,它不是文本,因此此 If 表达式始终为 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。多种方式解决这个问题,你需要考虑让用户纠正错误的方式。