C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 46 を表示中)
■24827 / )  Re[3]: 方向キーによるボタンコントロールの遷移制御について
□投稿者/ 魔界の仮面弁士 (842回)-(2008/09/10(Wed) 15:17:29)
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

返信 編集キー/


管理者用

- Child Tree -