【问题标题】:vb.net Using the drag and drop operation using imagesvb.net 使用图片的拖放操作
【发布时间】:2016-03-09 16:37:27
【问题描述】:

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

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

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

这是我拖放值的方法

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

【问题讨论】:

  • 您的DoDragDropControl 作为要拖放的数据(与问题标题匹配)启动,但您在dragenter 中测试DataFormats.Text

标签: vb.net drag-and-drop picturebox flowlayoutpanel


【解决方案1】:
    ' 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)

或者你会创建一个新的并分配 Image 属性,将 pb.Image 设置为 Nothing。有多种方法可以解决这个问题,您需要考虑让用户纠正错误的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-12
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多