拖放 vb.net
Drag-drop in vb.net
我是 visual basic 的新手,我跟着一篇文章写了这段代码来拖放图像。
但是我想加一个if语句来控制拖放,这样如果图片1到图片框2,就会提示放错地方了。
我的代码:
Public Class Form1
Private Source As PictureBox = Nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each PB As PictureBox In Me.Controls.OfType(Of PictureBox)()
PB.AllowDrop = True
AddHandler PB.MouseMove, AddressOf PBs_MouseMove
AddHandler PB.DragEnter, AddressOf PBs_DragEnter
AddHandler PB.DragDrop, AddressOf PBs_DragDrop
AddHandler PB.DragOver, AddressOf PBs_DragOver
Next
End Sub
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(ByVal sender As Object, ByVal 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 ' store the current image
PB.Image = e.Data.GetData(DataFormats.Bitmap) ' change it to the dropped image
If e.Effect = DragDropEffects.Move Then
If Not (PB Is Source) Then
Source.Image = tmpImage ' put the stored image in the source picturebox (swap)
End If
End If
End Sub
End Class
您可以尝试更改您的 PBs_DragDrop 代码来检查哪个 PictureBox 被拖放到这样
Private Sub PBs_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
If PB Is PictureBox2 Then
'handle mistake here
Else
Dim tmpImage As Image = PB.Image ' store the current image
PB.Image = e.Data.GetData(DataFormats.Bitmap) ' change it to the dropped image
If e.Effect = DragDropEffects.Move Then
If Not (PB Is Source) Then
Source.Image = tmpImage ' put the stored image in the source picturebox (swap)
End If
End If
End If
End Sub
我是 visual basic 的新手,我跟着一篇文章写了这段代码来拖放图像。
但是我想加一个if语句来控制拖放,这样如果图片1到图片框2,就会提示放错地方了。
我的代码:
Public Class Form1
Private Source As PictureBox = Nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each PB As PictureBox In Me.Controls.OfType(Of PictureBox)()
PB.AllowDrop = True
AddHandler PB.MouseMove, AddressOf PBs_MouseMove
AddHandler PB.DragEnter, AddressOf PBs_DragEnter
AddHandler PB.DragDrop, AddressOf PBs_DragDrop
AddHandler PB.DragOver, AddressOf PBs_DragOver
Next
End Sub
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(ByVal sender As Object, ByVal 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 ' store the current image
PB.Image = e.Data.GetData(DataFormats.Bitmap) ' change it to the dropped image
If e.Effect = DragDropEffects.Move Then
If Not (PB Is Source) Then
Source.Image = tmpImage ' put the stored image in the source picturebox (swap)
End If
End If
End Sub
End Class
您可以尝试更改您的 PBs_DragDrop 代码来检查哪个 PictureBox 被拖放到这样
Private Sub PBs_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
Dim PB As PictureBox = DirectCast(sender, PictureBox)
If PB Is PictureBox2 Then
'handle mistake here
Else
Dim tmpImage As Image = PB.Image ' store the current image
PB.Image = e.Data.GetData(DataFormats.Bitmap) ' change it to the dropped image
If e.Effect = DragDropEffects.Move Then
If Not (PB Is Source) Then
Source.Image = tmpImage ' put the stored image in the source picturebox (swap)
End If
End If
End If
End Sub