|
■No24825 (トトロ さん) に返信
> Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
「KeyDown イベント」ではなく、「PreviewKeyDown イベント」を使ってみるとか。
'
' ┏━━━━━━━━━━┓┏━━━━━━━━━━┓
' ┃Button1 (TabIndex=0)┃┃Button4 (TabIndex=3)┃
' ┗━━━━━━━━━━┛┗━━━━━━━━━━┛
' ┏━━━━━━━━━━┓┏━━━━━━━━━━┓
' ┃Button2 (TabIndex=1)┃┃Button5 (TabIndex=4)┃
' ┗━━━━━━━━━━┛┗━━━━━━━━━━┛
' ┏━━━━━━━━━━┓┏━━━━━━━━━━┓
' ┃Button3 (TabIndex=2)┃┃Button6 (TabIndex=5)┃
' ┗━━━━━━━━━━┛┗━━━━━━━━━━┛
'
Public Class Form1
Private buttonNavi As New Navigators(Of Button)()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
buttonNavi.Add(Button1, Nothing, Button4, Button2, Nothing)
buttonNavi.Add(Button2, Button1, Button5, Button3, Nothing)
buttonNavi.Add(Button3, Button2, Button6, Nothing, Nothing)
buttonNavi.Add(Button4, Nothing, Nothing, Button5, Button1)
buttonNavi.Add(Button5, Button4, Nothing, Button6, Button2)
buttonNavi.Add(Button6, Button5, Nothing, Nothing, Button3)
End Sub
Private Class Navigators(Of T As Control)
Private list As New List(Of Navigator)()
Public Sub Add(ByVal target As T, ByVal up As T, ByVal right As T, ByVal down As T, ByVal left As T)
list.Add(New Navigator(target, up, right, down, left))
End Sub
Private Class Navigator
Private self As T
Private up, right, down, left As T
Private Sub PreviewKeyDown(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs)
Select Case e.KeyCode
Case Keys.Up
up.Focus()
e.IsInputKey = True
Case Keys.Right
right.Focus()
e.IsInputKey = True
Case Keys.Down
down.Focus()
e.IsInputKey = True
Case Keys.Left
left.Focus()
e.IsInputKey = True
End Select
End Sub
Public Sub New(ByVal target As T, ByVal up As T, ByVal right As T, ByVal down As T, ByVal left As T)
Me.self = target
#If VBC_VER < 9 Then
Me.up = IIf(IsNothing(up), target, up)
Me.right = IIf(IsNothing(right), target, right)
Me.down = IIf(IsNothing(down), target, down)
Me.left = IIf(IsNothing(left), target, left)
#Else
Me.up = If(up, target)
Me.right = If(right, target)
Me.down = If(down, target)
Me.left = If(left, target)
#End If
AddHandler target.PreviewKeyDown, AddressOf PreviewKeyDown
End Sub
End Class
End Class
End Class
|