在vb.net中使用箭头键导航

时间:2012-12-21 02:22:47

标签: vb.net

我正在尝试使用箭头键(向上/向下)导航控件。
要尝试我的示例,只需创建一个新的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将“run out out”控制到控件上?

但是...

如果我用TAB键通过控件传递一次之后用箭头导航变好,那么焦点矩形就会出现,一切正常。

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

1 个答案:

答案 0 :(得分:1)

我找到了一个解决方案,可以通过代码“按预期”完成工作: C# code

这是我对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()

一次就够了:)。