【发布时间】: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