我有一个带有几个TabPages的TabControl,每个TabPages包含一系列PictureBox。我希望能够使用这些TabPages中的箭头键进行导航。当用户选择时,我尝试将.Focus()
提供给TabPage,然后捕获PreviewKeyDown
和KeyDown
事件,但它不起作用。我在PreviewKeyDown
事件处理程序中放置了一个断点,并且它不是事件到达的。
以下是TabPage的激活:
Sub TilesetsTab_Selected(ByVal sender As Object, ByVal e As TabControlEventArgs) Handles TilesetsTab.Selected
Dim myTab As TabControl = CType(sender, TabControl)
If myTab.TabCount = 0 Then Exit Sub
Active.Tileset = Project.Tileset(CInt(myTab.SelectedTab.Tag))
Active.Tileset.Tab.Focus()
End Sub
这是Key事件处理程序
Sub Tab_KeyPreviewDown(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs) Handles Tab.PreviewKeyDown
If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then e.IsInputKey = True
End Sub
Sub Tab_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Tab.KeyDown 'BUG
Dim TilesWidth As Integer = Me.Tab.Width \ Me.ScreenSize
Select Case e.KeyCode
Case Keys.Left
If Active.Tile.Index = 0 Then Exit Sub
Me.Tile(Active.Tile.Index - 1).Selected()
Case Keys.Right
If Active.Tile.Index = Project.Tileset.Count - 1 Then Exit Sub
Me.Tile(Active.Tile.Index + 1).Selected()
Case Keys.Up
If Active.Tile.Index < TilesWidth Then Exit Sub
Me.Tile(Active.Tile.Index - TilesWidth).Selected()
Case Keys.Down
If Active.Tile.Index + TilesWidth > Active.Tileset.Count - 1 Then Exit Sub
Me.Tile(Active.Tile.Index + TilesWidth).Selected()
End Select
End Sub
为什么我不能在TabPage上触发KeyDown事件?
答案 0 :(得分:0)
我不知道为什么昨天某人发布的答案(以及我发表的评论)被删除了,但据说TabPages无法集中注意力,而且仅限于TabControl。根据该建议,我将PreviewKeyDown
和KeyDown
事件处理程序转移到了TabControl事件,并且它有效。
我在TabPages之间失去了一些箭头导航,但它适合我。