使用vb.net的拖放操作使用图像
我目前正在尝试简化我们在工作中使用的工具。为此,我想使用拖放方法。这个工具基本上就像用三种不同的积木建造一座塔。在顶部有三个不同块的图像,下面是一个流程布局面板。目标是按所需顺序将块拖动到“流布局”面板中 这是一张代表起始位置的快速图像。(我只是想澄清一下。) 这对我来说是个棘手的部分。我只使用拖放方法将值从一个文本框拖放到另一个文本框中。现在我需要复制图像对象并将其添加到flow布局面板中 这是我用来拖放值的方法使用vb.net的拖放操作使用图像,vb.net,drag-and-drop,picturebox,flowlayoutpanel,Vb.net,Drag And Drop,Picturebox,Flowlayoutpanel,我目前正在尝试简化我们在工作中使用的工具。为此,我想使用拖放方法。这个工具基本上就像用三种不同的积木建造一座塔。在顶部有三个不同块的图像,下面是一个流程布局面板。目标是按所需顺序将块拖动到“流布局”面板中 这是一张代表起始位置的快速图像。(我只是想澄清一下。) 这对我来说是个棘手的部分。我只使用拖放方法将值从一个文本框拖放到另一个文本框中。现在我需要复制图像对象并将其添加到flow布局面板中 这是我用来拖放值的方法 Private MouseIsDown As Boolean = False
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)
或者创建一个新的,并指定Image属性,将pb.Image设置为Nothing。要做到这一点,有多种方法,您需要考虑如何让用户纠正错误。您的
DoDragDrop
以控件作为拖放数据(与问题标题匹配)启动,但您需要在dragenter中测试数据格式.Text
。