我想创建一个可以应用于我的动态子,我有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
答案 0 :(得分:0)
您可以动态地将事件附加到您的所有图片框。
参见此示例
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个不同的事件处理程序。
答案 1 :(得分:0)
在Drop中,您正确地将Target中的Tag存储在temp变量中,但只在Source中设置了Tag,而不是在Target中设置了Tag(你做了半个交换)。
所以你错过了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