如何为 Windows 窗体 TabControl 设置键盘快捷键? [英] How can I set up keyboard shortcuts for a Windows Forms TabControl?

查看:42
本文介绍了如何为 Windows 窗体 TabControl 设置键盘快捷键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以为 Visual Studio 2010 中的选项卡控件中的选项卡设置键盘快捷键?有我可以设置的属性吗?

Is there an easy way to set a keyboard shortcut for a tab in a tab control in Visual Studio 2010? Is there some property I can set?

我在网上看,但我看到的所有文章都非常混乱.

I looked online, but all the articles I saw were very confusing.

推荐答案

不幸的是,没有任何这样的属性,但是实现这个功能也不是很困难.有两种方法值得考虑,具体取决于您的应用程序设计.

Unfortunately, there isn't any such a property, but implementing this functionality doesn't have to be difficult, either. There are two ways worth considering, depending on the design of your application.

  1. 如果承载 TabControl 的表单已经使用了菜单系统,则设置起来几乎是微不足道的.首先,您需要添加一个菜单命令,用于切换到 TabControl 中的每个 TabPage.然后,您可以简单地向该菜单项添加一个键盘快捷键(MenuItem/ToolStripMenuItem 的一个简单属性),这样无论何时按下那个键,执行那个菜单命令,切换到适当的TabPage.

  1. If the form that hosts the TabControl already uses a menu system, it's almost trivial to set up. First, you need to add a menu command that switches to each TabPage in your TabControl. Then, you can simply add a keyboard shortcut to that menu item (which is a simple property of a MenuItem/ToolStripMenuItem), so that whenever that key is pressed, that menu command is executed, which switches to the appropriate TabPage.

然而,菜单系统可能并不适合每种形式.如果是这种情况,您将不得不做更多的工作.基本上,您需要将承载 TabControl 的表单的 KeyPreview 属性设置为 True 并检测要用于切换选项卡的键盘快捷键.

However, a menu system might not be appropriate for every form. If that's the case, you're going to have to do a bit more work. Basically, you need to set the KeyPreview property of the form that hosts your TabControl to True and detect the keyboard shortcuts you want to use to switch tabs.

设置表单的KeyPreview 属性 允许该表单在将这些事件传递给具有焦点的控件之前接收关键事件.这对于此方法的工作至关重要,否则,表单的 KeyDown 事件处理程序中的代码将永远不会检测到您想要捕获的击键.只有当表单处理完每个击键后,它们才会被传递到通常会接收它们的控件上.

Setting a form's KeyPreview property allows that form to receive key events before those events are passed on to the control that has the focus. This is crucial for this method to work, because otherwise, your code in the form's KeyDown event handler will never detect the keystrokes that you want to trap. Only once the form has finished processing each keystroke will they be passed onto the control that would ordinarily receive them.

因此,一旦您设置了此属性,您就需要为表单的KeyDown 事件 监视您要使用的任何键盘快捷键,然后在检测到这些键之一时相应地切换选项卡被按下.否则,您无需执行任何操作.

So, once you've set this property, you need to add code to the handler for your form's KeyDown event that watches for whichever keyboard shortcuts you want to use, and then switches tabs accordingly if it detects that one of those keys is pressed. Otherwise, you don't have to do anything.

例如,如果您的表单上有三个 TabPages,您可能决定 F2 将切换到第一个选项卡,F3 将切换到第二个,F4 将切换到第三个(尽管很明显,您可以使用任何您想要的键).然后,您可以将以下代码添加到表单的 KeyDown 事件处理程序中,以检测这些键被按下并采取相应措施:

For example, if you have three TabPages on your form, you might decide that F2 will switch to the first tab, F3 will switch to the second, and F4 will switch to the third (although, obviously, you could use whatever keys you wanted). You would then add the following code to your form's KeyDown event handler that detects those keys being depressed and acts accordingly:

 Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
       Case Keys.F2
          'Switch to the first tab page
          MyTabControl.SelectedIndex = 0
       Case Keys.F3
          'Switch to the second tab page
          MyTabControl.SelectedIndex = 1
       Case Keys.F4
          'Switch to the third tab page
          MyTabControl.SelectedIndex = 2
    End Select
 End Sub

这篇关于如何为 Windows 窗体 TabControl 设置键盘快捷键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆