2012-12-21 150 views
0

我試圖通過帶有箭頭鍵的控件進行導航(上/下)。
要嘗試我的示例,只需創建一個新的form1並將其粘貼到它。使用vb.net中的箭頭鍵導航

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鍵一次傳球后的箭導航變好,焦點矩形出現的,一切都OK。

這裏有什麼問題? 在程序啓動後立即使用箭頭進行導航的操作與使用製表鍵相同?

+0

的可能重複[爲什麼焦點矩形不顯示,直到Tab鍵按下?(http://stackoverflow.com/questions/9226433/why-is-focus-rectangle-not -show-until-tab-key-pressed) –

+0

現在看,可能是重複的,但這裏是完全可行的代碼,用於調查,我的示例不包含導致問題的單選按鈕。還有Windows 7! –

+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() 

一次就夠了:)