简体   繁体   English

VB.net TextBox不会与Keydown事件一起被focus()

[英]VB.net TextBox won't be focused() with Keydown event

I want to set a focus to the specific textbox when I press ALT+SOME KEY. 我想在按ALT + SOME KEY时将焦点设置到特定的textbox

However, when I press the shortcut key combination, it focuses something else. 但是,当我按下快捷键组合时,它会聚焦其他内容。 Even I pressed CTRL+P, the txtAuthor got no focus at all while some strange Button control or another textbox got focus. 即使我按了CTRL + P, txtAuthor也完全没有焦点,而某些奇怪的Button control或另一个textbox也没有焦点。

I checked that there is no duplicated key combinations. 我检查了是否有重复的按键组合。 eg: &Press Here .. 例如:&按这里..

plus : It is confirmed by Debugging that when I press Alt+Q, for example, the txtQueue.Focus() code below is executed. plus:通过调试确认,例如,当我按Alt + Q时,将执行下面的txtQueue.Focus()代码。 But, somehow txtQueue won't be focused. 但是,以某种方式txtQueue不会被关注。

plus : KeyPreview is set to True 加: KeyPreview设置为True

Private Sub frmEdit_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown


    'Alt+Q
    If e.KeyCode = Keys.Q AndAlso e.Modifiers = Keys.Alt Then
        txtQueue.Focus()
        Exit Sub
    End If

    'Alt+D
    If e.KeyCode = Keys.D AndAlso e.Modifiers = Keys.Alt Then
        txtDynamic.Focus()
        Exit Sub
    End If

    'Alt+K
    If e.KeyCode = Keys.K AndAlso e.Modifiers = Keys.Alt Then
        txtTime.Focus()
        Exit Sub
    End If

    'Alt+P
    If e.KeyCode = Keys.P AndAlso e.Modifiers = Keys.Alt Then
        txtAlgorithm.Focus()
        Exit Sub
    End If


End Sub

THank you! 谢谢!

I would set breakpoint as you seem to have done to check that the correct focus is set but then continue stepping through the code (F8 in VStudio) to check that nothing then moves the focus elsewhere. 正如您似乎已经做过的那样,我将设置断点来检查是否设置了正确的焦点,但是然后继续单步执行代码(VStudio中为F8)以检查是否没有任何事情将焦点移到其他位置。 Does the same control always get focus? 相同的控件是否总是获得焦点?

Also I would change your code to incorporate a select case... 我也将更改您的代码以合并选择的案例...

Private Sub frmEdit_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

If e.Modifiers = Key.Alt Then

    Select Case (e.KeyCode)

        'Alt+Q
        Case Keys.Q 
            txtQueue.Focus()
            Exit Select

        'Alt+D
        Case Keys.D 
            txtDynamic.Focus()
            Exit Select

        'Alt+K
        Case Keys.K 
            txtTime.Focus()
            Exit Select

        'Alt+P
        Case Keys.P 
            txtAlgorithm.Focus()
            Exit Select
    End Select

End If

End Sub

overriding the ProcessCmdKey() works! 重写ProcessCmdKey()起作用!

I don't know the reason, however, intermittently ProcessCmdKey() doesn't work even though I clicked the Form, Buttons inside the Form etc... 我不知道原因,但是,即使我单击了窗体,窗体内的按钮等,间歇地ProcessCmdKey()也无法工作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM