KeyDown 事件未使用 .NET WinForms 触发? [英] KeyDown event not firing with .NET WinForms?
问题描述
我已经有 KeyPreview
在表单属性中设置为true
我正在开发一个小程序,但我遇到了一个问题,当我按下并释放任何箭头键时,它在分组框中的某些控件似乎没有触发表单上的 KeyDown 事件,只是KeyUp 事件.我的代码有什么问题可能导致这种情况吗?
I'm working on a small program, and I'm having a problem where it seems that some of the controls on it inside groupboxes are not triggering the KeyDown event on my form when I press and release any arrow key, just the KeyUp event. Is there something wrong with my code that might be causing this?
具体来说,我在表单上启用了 KeyPreview,并在两个子程序中的 e.SuppressKeyPress = True
上设置了断点,只有 frmMain_KeyUp 的一个断点.
Specifically, I've enabled KeyPreview on the form, and set breakpoints on e.SuppressKeyPress = True
in both subroutines, and only the one for frmMain_KeyUp hits the breakpoint.
我添加了两个 GroupBox 事件,希望可以缓解这个问题,但没有这样的运气.但是,我在表单上有一个自定义控件,专门编码为忽略这些按键,并且代码按预期工作.
I added in the two GroupBox events hoping that might mitigate the issue, but no such luck. However, I have a custom control on the form that is specifically coded to ignore these keypresses, and the code works as expected on it.
Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown, GroupBox1.KeyDown, GroupBox2.KeyDown
e.SuppressKeyPress = True
Select Case e.KeyCode
Case Keys.Left
ScrollDir = ScrollDir Or 1
Case Keys.Right
ScrollDir = ScrollDir Or 2
Case Keys.Down
ScrollDir = ScrollDir Or 4
Case Keys.Up
ScrollDir = ScrollDir Or 8
Case Else
e.SuppressKeyPress = False
End Select
tScroll.Enabled = True
tScroll_Tick(Nothing, Nothing)
End Sub
Private Sub frmMain_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp, GroupBox1.KeyUp, GroupBox2.KeyUp
e.SuppressKeyPress = True
Select Case e.KeyCode
Case Keys.Left
ScrollDir = ScrollDir And (Not 1)
Case Keys.Right
ScrollDir = ScrollDir And (Not 2)
Case Keys.Down
ScrollDir = ScrollDir And (Not 4)
Case Keys.Up
ScrollDir = ScrollDir And (Not 8)
Case Else
e.SuppressKeyPress = False
End Select
If ScrollDir = 0 Then tScroll.Enabled = False
End Sub
用户控件中忽略"按键的代码如下:
The code in the user control that "ignores" keypresses is as such:
Private Sub TileDropDown_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = 40 OrElse e.KeyValue = 38 OrElse e.KeyValue = 39 OrElse e.KeyValue = 37 Then
e.SuppressKeyPress = True
End If
End Sub
推荐答案
某些控件在 keydown 事件中拦截箭头键,但在 keyup 事件中不拦截.一种解决方案是派生控件类并覆盖 ProcessCmdKey:
Some controls intercept the arrow keys in the keydown event, but not in the keyup event. One solution is to derive the control class and override ProcessCmdKey:
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keydata As Keys) As Boolean
If keydata = Keys.Right Or keydata = Keys.Left Or keydata = Keys.Up Or keydata = Keys.Down Then
OnKeyDown(New KeyEventArgs(keydata))
ProcessCmdKey = True
Else
ProcessCmdKey = MyBase.ProcessCmdKey(msg, keydata)
End If
End Function
这篇关于KeyDown 事件未使用 .NET WinForms 触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!