【发布时间】:2018-11-13 05:07:08
【问题描述】:
我使用以下代码来处理表单中某些控件的定位;
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
'Sub detects which arrow key is pressed
Dim strControlName As String
' Get the name of the control
strControlName = Me.ActiveControl.Name
Dim aControl = Me.Controls.Item(strControlName)
If strControlName <> "PrintButton" Then
If keyData = Keys.Up Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y - 1)
Return True
End If
'detect down arrow ke
If keyData = Keys.Down Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y + 1)
Return True
End If
'detect left arrow key
If keyData = Keys.Left Then
aControl.Location = New Point(aControl.Location.X - 1, aControl.Location.Y)
Return True
End If
'detect right arrow key
If keyData = Keys.Right Then
aControl.Location = New Point(aControl.Location.X + 1, aControl.Location.Y)
Return True
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
我还有一个允许将图像拖放到其中的 PictureBox;
Private Sub pbSig_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length <> 0 Then
Try
picbox.Image = Image.FromFile(files(0))
pbSig.ImageLocation = files(0)
Catch ex As Exception
MessageBox.Show("Problem opening file ")
End Try
End If
End Sub
Private Sub pbSig_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
有没有一种方法可以使 PictureBox 使用箭头键“可移动”?我不能在表单上使用 KeyPress 事件,因为我已经在其他地方使用它。我希望我可以将焦点放在 PictureBox 上或允许用户执行“+箭头”事件。
另外,如果我让 PictureBox 移动,丢弃的图像会随之移动吗?
【问题讨论】:
-
您的代码工作正常。您只有一个问题:PictureBox 不能成为活动控件(不使用默认类样式,它需要
ControlStyles.Selectable)。如果您将aControl.Location = (...)更改为 PictureBox 的名称(例如,pbSig.Location = (...),那么 PictureBox 将移动并接受放置(假设您在某处有set pbSig.AllowDrop = True),设置新图像。您在 @ 中有错字987654327@ =>pbSig.ImageLocation = files(0)应该是picbox.ImageLocation = files(0)。
标签: vb.net