vb.net使用图像进行拖放操作 [英] vb.net Using the drag and drop operation using images

查看:315
本文介绍了vb.net使用图像进行拖放操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently trying to simplify a tool that we use at work. For this I would like to use the drag and drop method. The tool is basically like building a tower using three different kind of blocks. At the top there are the three images of the different blocks below is a flow-layout-panel. The goal is to drag in a desired order the blocks into the flow-layout-panel.

这是一张代表开始位置的快速图像. (只是要清楚.)

Here is a quick image that would represent the start position. (Just to be clear.)

这对我来说是棘手的部分.我仅使用拖放方法将值从一个文本框中拖放到另一个文本框中.现在,我需要复制图像对象并将其添加到flow-layout-panel中.

This for me is the tricky part. I only used the drag and drop method to drop values from one text-box into another one. Now I need to copy the image-object and add it into the flow-layout-panel.

这是我遵循的拖放值的方法

This is the method I followed to drag and drop values

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

现在,对于下一步,我应该对图像执行相同的操作,这是我的尝试:

Now for the next step I should do the same for the images, here is my try:

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项目拖到面板上,我只会得到不能放下的符号..所以这有点卡住了.有人可以向我提供一些有关如何执行此操作的信息吗?还是给我一些指示?

But if I do this, and drag the picturebox1 item onto the panel, I only get the can't drop symbol.. So this is where i'm kinda stuck. Can someone please provide me with some info how to do this? Or give me some pointers?

推荐答案

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

那是不正确的.现在,您正在拖动PictureBox对象,因为它不是文本,所以此If表达式始终为False.仅当您看到一个PictureBox对象被拖动时,您才会感到高兴.像这样:

That is not correct. You are now dragging a PictureBox object, it is not text so this If expression is always going to be False. You are only happy when you see a PictureBox object being dragged. Like this:

    If (e.Data.GetDataPresent(GetType(PictureBox))) Then

DragDrop事件处理程序中存在相同的问题,它需要类似于:

Same issue in your DragDrop event handler, it needs to resemble:

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

或者您将创建一个新对象并分配Image属性,将pb.Image设置为Nothing.解决此问题的方法多种多样,您需要考虑让用户纠正错误的方法.

Or you'd create a new one and assign the Image property, setting pb.Image to Nothing. Multiple ways to go about this, you need to think about the way you let the user correct mistakes.

这篇关于vb.net使用图像进行拖放操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆