0

我正在尝试使用箭头键(上/下)通过控件进行导航。
要尝试我的示例,只需创建一个新的 form1 并将此代码粘贴到其中。

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim tb As New TextBox
    Dim cb As New CheckBox
    Dim cbb As New ComboBox
    Dim b1 As New Button
    Dim b2 As New Button

    With Me
        .KeyPreview = True
        .Size = New Size(350, 200)
        With .Controls
            .Add(tb)
            With tb
                .TabIndex = 0
                .Location = New Point(95, 20)
                .Text = "This is"
            End With
            .Add(cb)
            With cb
                .TabIndex = 1
                .Location = New Point(95, 50)
                .Checked = True
                .Text = "Example checkbox"
                .AutoSize = True
            End With
            .Add(cbb)
            With cbb
                .TabIndex = 2
                .Location = New Point(95, 80)
                .Text = "an Example"
                .DropDownStyle = ComboBoxStyle.DropDownList
            End With
            .Add(b1)
            With b1
                .TabStop = False
                .Location = New Point(90, 130)
                .Text = "Nothing"
            End With
            .Add(b2)
            With b2
                .TabStop = False
                .Location = New Point(170, 130)
                .Text = "Exit"
                AddHandler b2.Click, AddressOf b2_Click
            End With
        End With
    End With
End Sub

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Up Then
        e.Handled = True
        Me.SelectNextControl(Me.ActiveControl, False, True, True, True)
    End If

    If e.KeyCode = Keys.Down Then
        e.Handled = True
        Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
    End If
End Sub

Private Sub b2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Me.Close()
End Sub

End Class

发生什么了?
当启动一个程序并用箭头导航时,控件周围没有“焦点矩形”,在某些情况下,焦点“跑出来”到带有 tabstop = false 的控件?

但...

如果我用 TAB 键通过控件后,用箭头导航变得很好,焦点矩形出现,一切正常。

这里可能有什么问题?在程序启动后立即使用箭头导航的行为与使用 tab 键的行为相同,该怎么办?

4

1 回答 1

1

我在这里找到了一种解决方案,可以“通过代码”按预期工作: C# 代码

这是我对 VB 的翻译。
1)在您的一些公共模块中添加导入...

Imports System.Runtime.InteropServices

2)将此声明放在同一模块中:

<DllImport("user32.dll")> _
Private Sub SystemParametersInfo(ByVal uiAction As UInteger, ByVal uiParam As UInteger, ByRef pvParam As Integer, ByVal fWinIni As UInteger)
End Sub

' Constants used for User32 calls. 
Const SPI_SETKEYBOARDCUES As UInteger = &H100B

3)将此公共功能放在同一模块中:

''' <summary> 
''' Change the setting programmatically 
''' for keyboard shortcut Issue 
''' </summary> 
Public Sub GetAltKeyFixed()
    Dim pv As Integer = 1
    ' Call to systemparametersinfo to set true of pv variable.

    SystemParametersInfo(SPI_SETKEYBOARDCUES, 0, pv, 0)
    'Set pvParam to TRUE to always underline menu access keys, 
End Sub

4)从程序的开始位置(比如Form1)只需调用:

GetAltKeyFixed()

一次就够了 :)

于 2012-12-21T09:35:51.160 回答