【问题标题】:Making a swap Sub on pictureboxes with drag/drop in vb.net在 vb.net 中通过拖放在图片框上进行交换子
【发布时间】:2015-03-13 07:26:28
【问题描述】:

我想制作一个可以应用于我的动态子组件,我有一个带有图片框的 3x3 矩阵,我想用拖放样式更改它们中的图像。

我找到了这段代码,我知道它是如何工作的,但我想让它动态化,这样它就可以在表单中的所有 9 个 picbox 上工作,而不仅仅是前两个。

Dim firstimage As Image
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PictureBox2.AllowDrop = True
End Sub
Private Sub pictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
    If e.Button = MouseButtons.Left Then
        PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.All)
    End If
End Sub
Private Sub pictureBox2_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles PictureBox2.DragEnter
    firstimage = PictureBox2.Image
    If e.Data.GetDataPresent(DataFormats.Bitmap) Then
        e.Effect = DragDropEffects.Copy
    Else
        e.Effect = DragDropEffects.None
    End If
End Sub
Private Sub pictureBox2_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles PictureBox2.DragDrop
    If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
        PictureBox2.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
        PictureBox1.Image = firstimage 'Set picturebox1 to the stored image
    End If
End Sub

我已经阅读并使用了 idle_mind 答案,它的工作非常好,这正是我想要的,但我需要再更新一次才能让它为我工作。 我不仅需要更改图像,还需要更改框的标签(像图像一样交换它们。) 我在这里所做的是只有第一个得到标签,但第二个你不能解释我需要在哪里添加代码更改。

Private Sub PBs_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim PB As PictureBox = DirectCast(sender, PictureBox)
        If Not IsNothing(PB.Image) AndAlso e.Button = Windows.Forms.MouseButtons.Left Then
            Source = PB
            PB.DoDragDrop(PB.Image, DragDropEffects.Copy Or DragDropEffects.Move)
        End If
    End Sub

    Private Sub PBs_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent(DataFormats.Bitmap) Then
            If My.Computer.Keyboard.CtrlKeyDown Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.Move
            End If
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub PBs_DragOver(sender As Object, e As DragEventArgs)
        If e.Data.GetDataPresent(DataFormats.Bitmap) Then
            If My.Computer.Keyboard.CtrlKeyDown Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.Move
            End If
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub PBs_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        Dim PB As PictureBox = DirectCast(sender, PictureBox)
        Dim tmpImage As Image = PB.Image
        Dim tmptag As Integer = PB.Tag ` The Tag Temp
        PB.Image = e.Data.GetData(DataFormats.Bitmap)
        If e.Effect = DragDropEffects.Move Then
            If Not (PB Is Source) Then
                Source.Image = tmpImage
                Source.Tag = tmptag ` the first picbox gets the new tag
            End If
        End If
    End Sub

【问题讨论】:

  • 我无法理解,您的代码适用于两个图片框,但无法推断它适用于 9 个图片框?
  • 该代码存在严重缺陷。 pictureBox1 将在每次鼠标移动时启动拖放交换盛宴。拖动通常被认为是在按住按钮的同时移动鼠标。
  • 在这个帖子中查看我的answer here
  • Mathemats 是的,我有这个代码,但它适用于 2 个特定的图片框,我希望它适用于我在表单中拥有的所有图片框。 Idle_Mind 我看到了代码,它很棒,但它只是移动或复制,我需要它在 picA 和 picB 之间交换,它应该适用于所有这些,你能为我改变它吗?
  • 在该线程中阅读我的整个答案。那里也有代码可以交换它们......

标签: vb.net visual-studio-2012 drag-and-drop picturebox


【解决方案1】:

您可以将事件动态附加到所有图片框。

看这个例子

Public Class Form1


    Dim firstimage As Image
    Dim pold As PictureBox
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.AllowDrop = True
        PictureBox2.AllowDrop = True
        PictureBox3.AllowDrop = True
        PictureBox4.AllowDrop = True
        PictureBox5.AllowDrop = True
        AddHandler PictureBox1.MouseMove, AddressOf p_MouseMove
        AddHandler PictureBox1.DragEnter, AddressOf p_DragEnter
        AddHandler PictureBox1.DragDrop, AddressOf p_DragDrop
        AddHandler PictureBox1.MouseEnter, AddressOf P_MouseEnter

        AddHandler PictureBox2.MouseMove, AddressOf p_MouseMove
        AddHandler PictureBox2.DragEnter, AddressOf p_DragEnter
        AddHandler PictureBox2.DragDrop, AddressOf p_DragDrop
        AddHandler PictureBox2.MouseEnter, AddressOf P_MouseEnter

        AddHandler PictureBox3.MouseMove, AddressOf p_MouseMove
        AddHandler PictureBox3.DragEnter, AddressOf p_DragEnter
        AddHandler PictureBox3.DragDrop, AddressOf p_DragDrop
        AddHandler PictureBox3.MouseEnter, AddressOf P_MouseEnter

        AddHandler PictureBox4.MouseMove, AddressOf p_MouseMove
        AddHandler PictureBox4.DragEnter, AddressOf p_DragEnter
        AddHandler PictureBox4.DragDrop, AddressOf p_DragDrop
        AddHandler PictureBox4.MouseEnter, AddressOf P_MouseEnter

        AddHandler PictureBox5.MouseMove, AddressOf p_MouseMove
        AddHandler PictureBox5.DragEnter, AddressOf p_DragEnter
        AddHandler PictureBox5.DragDrop, AddressOf p_DragDrop
        AddHandler PictureBox5.MouseEnter, AddressOf P_MouseEnter
    End Sub

    Private Sub P_MouseEnter(sender As Object, e As EventArgs)
        Dim p As PictureBox = sender
        pold = p
        Me.Text = pold.Name
    End Sub
    Private Sub p_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
        Dim p As PictureBox = sender
        If e.Button = MouseButtons.Left Then
            p.DoDragDrop(p.Image, DragDropEffects.All)

        End If
    End Sub
    Private Sub p_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
        Dim p As PictureBox = sender
        firstimage = p.Image
        If e.Data.GetDataPresent(DataFormats.Bitmap) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub p_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
        Dim p As PictureBox = sender
        If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
            p.Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
            pold.Image = firstimage 'Set picturebox1 to the stored image
        End If
    End Sub
End Class

只需添加任意数量的图片框并为其附加 4 个不同的事件处理程序。

【讨论】:

    【解决方案2】:

    在 Drop 中,您正确地将来自 Target 的 Tag 存储在一个临时变量中,但只在 Source 中设置了 Tag,而不是 Target(您做了一半的交换)。

    所以你错过了PB.Tag = Source.Tag

    改成:

    Private Sub PBs_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        Dim PB As PictureBox = DirectCast(sender, PictureBox)
        Dim tmpImage As Image = PB.Image
        Dim tmptag As Integer = PB.Tag ` The Tag Temp
        PB.Image = e.Data.GetData(DataFormats.Bitmap)
        PB.Tag = Source.Tag
        If e.Effect = DragDropEffects.Move Then
            If Not (PB Is Source) Then
                Source.Image = tmpImage
                Source.Tag = tmptag ` the first picbox gets the new tag
            End If
        End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2013-07-14
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多