在 vb.net 中使用 drag/drop 在图片框上制作交换子

Making a swap Sub on pictureboxes with drag/drop in vb.net

我想制作一个可以应用于我的动态子,我有一个带图片框的 3x3 矩阵,我想用 drag/drop 样式相互更改它们中的图像。

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

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

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

看这个例子

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 个不同的事件处理程序。

在 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