Top / TIPS / Enter キーで TAB 移動

実現するためには、2つの問題を解決しなければなりません。 すなわち、

に、尽きると思います。
さて、それぞれ、見ていきましょう。

Enter キーが押されたイベントの検出

(1) フィールドの KeyDown イベントで検出

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then
        (フィールドを移動する処理)
    End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        KeyAscii = 0                    ' Beep 音を鳴らさないようにする。
    End If
End Sub

(2) フォームの KeyPress イベントで検出

Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        (フィールドを移動する処理)
        KeyAscii = 0
    End If
End Sub

(3) デフォルトボタンで検出

Private Sub Form_Load()
    Command1.Left = -1 * Command1.Width - 100 ' 可視領域の外に
    Command1.Default = True
End Sub
Private Sub Command1_Click()
   (フィールドを移動する処理)
End Sub

フィールドを移動する方法

(1) SendKeys を使う

Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        SendKeys "{TAB}", True
        KeyAscii = 0
    End If
End Sub

(2) PostMessage API を使う

Private Const WM_KEYDOWN = &H100
Private Declare Function PostMessage _
       Lib "user32" _
       Alias "PostMessageA" _
       (ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        ByVal lParam As Long) As Long

Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        PostMessage Me.hwnd, WM_KEYDOWN, vbKeyTab, 0
        KeyAscii = 0
    End If
End Sub

(3) keybd_event を使う

Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_TAB = &H9
Private Declare Sub keybd_event _
                Lib "user32" _
                (ByVal bVk As Byte, _
                 ByVal bScan As Byte, _
                 ByVal dwFlags As Long, _
                 ByVal dwExtraInfo As Long)

Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        Call keybd_event(VK_TAB, 0, 0, 0)
        Call keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0)
        KeyAscii = 0
    End If
End Sub

(4) SetFocus を使う

Dim mctlField() As Control
Dim mlngIndex   As Long

Private Sub Form_Load()
   ReDim mctlField(2)
   Set mctlField(0) = Text1
   Set mctlField(1) = Text2
   Set mctlField(2) = Combo1
   Me.KeyPreview = True
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
   If KeyAscii = vbKeyReturn Then
       mctlField(GetNextIndex()).SetFocus
       KeyAscii = 0
   End If
End Sub

Private Function GetNextIndex() As Long
   Dim i As Long
   Dim lngActiveIndex As Long
   For i = LBound(mctlField) To UBound(mctlField)
       If ActiveControl.TabIndex = mctlField(i).TabIndex Then
           lngActiveIndex = i
           Exit For
       End If
   Next
   If lngActiveIndex = UBound(mctlField) Then
       GetNextIndex = 0
   Else
       GetNextIndex = lngActiveIndex + 1
   End If
End Function

(5) Enabled 切り替え

Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        With ActiveControl
            .Enabled = False
            .Enabled = True
        End With
        KeyAscii = 0
    End If
End Sub



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   最終更新のRSS
Last-modified: 2009-10-27 (火) 02:40:11 (2850d)