vb.net 如何在vb.net中制作快捷键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9267062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to make shortcut keys in vb.net
提问by user944591
I want to run specific code through shortcuts defined for my specific program. For example if I click F1or Ctrl+Cthen I want my program to show customers list. I have tried form's keydown event with following code
我想通过为我的特定程序定义的快捷方式运行特定代码。例如,如果我单击F1或Ctrl+C然后我希望我的程序显示客户列表。我已经使用以下代码尝试了表单的 keydown 事件
If e.KeyCode = Keys.F1 Then
Form6.button4.performclick()
End If
But this doesn't seem to work. Can anyone help me in this?
但这似乎不起作用。任何人都可以帮助我吗?
I'm using vb.net in visual studio 2005 to develop my application
我在 Visual Studio 2005 中使用 vb.net 来开发我的应用程序
回答by Apache
Try this:
尝试这个:
Private Sub frmCustomerDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' When the form loads, the KeyPreview property is set to True.
' This lets the form capture keyboard events before
' any other element in the form.
Me.KeyPreview = True
End Sub
Private Sub frmCustomerDetails_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If (e.Alt AndAlso (e.KeyCode = Keys.P)) Then
' When Alt + P is pressed, the Click event for the print
' button is raised.
RaiseEvent btnPrintCustomerDetails.Click
End If
End Sub
Although, you'd be better to create your own event and raise that instead of raising the button's click event.
虽然,您最好创建自己的事件并引发它,而不是引发按钮的点击事件。
回答by Barry Franklin
"An access key is an underlined character in the text of a menu, menu item, or the label of a control such as a button. With an access key, the user can "click" a button by pressing the ALT key in combination with the predefined access key. For example, if a button runs a procedure to print a form, and therefore its Text property is set to "Print," adding an ampersand before the letter "P" causes the letter "P" to be underlined in the button text at run time. The user can run the command associated with the button by pressing ALT+P. You cannot have an access key for a control that cannot receive focus."
“访问键是菜单、菜单项或控件(例如按钮)的标签文本中的带下划线的字符。使用访问键,用户可以通过按下 ALT 键与预定义的访问键。例如,如果一个按钮运行一个过程来打印表单,因此它的 Text 属性设置为“打印”,则在字母“P”之前添加一个与号会使字母“P”在运行时的按钮文本。用户可以通过按 ALT+P 运行与按钮关联的命令。对于无法获得焦点的控件,您不能拥有访问键。”
Try access keys?
尝试访问密钥?
回答by PresidentWesleyIII
Here is a very simple way to do something like that:
这是一个非常简单的方法来做这样的事情:
Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
If Keys.T + Keys.ControlKey Then
TextBox1.Text = RichTextBox1.SelectedText
End If
End Sub
This is from 2010 though.
不过这是2010年的。
回答by Robert Greiner
How to Capture Shortcut Keys in Visual Studio .NETThere are some VB examples there.
如何在 Visual Studio .NET 中捕获快捷键那里有一些 VB 示例。
回答by UnhandledExcepSean
I prefer to implement shortcuts by adding a menu for your shortcuts, add menu items for each shortcut, and then bind the shortcut to the menu items. This is nice because it documents the available shortcuts and makes the coding very simple.
我更喜欢通过为您的快捷方式添加菜单来实现快捷方式,为每个快捷方式添加菜单项,然后将快捷方式绑定到菜单项。这很好,因为它记录了可用的快捷方式并使编码变得非常简单。
回答by Amen Ayach
Your code is correct but you should set keyPreview = true
in your Form
您的代码是正确的,但您应该keyPreview = true
在表单中进行设置
回答by bawa904
I have encounterd with same situation, I used menustrip and asigned shortkeys and used performclick function and set the property of menustrip to visible false and it worked fine.
我遇到过同样的情况,我使用了 menustrip 和分配的快捷键,并使用了 performclick 功能并将 menustrip 的属性设置为可见的 false 并且它工作正常。
回答by jcaruso
I saw the answers listed and this is quite old but try this. The hotkeys are set as alt+d or alt+c as indicated. This difference in code between what they are giving you and what i am giving you is that this works even when the application is minimized.
我看到了列出的答案,这很旧,但试试这个。如图所示,热键设置为 alt+d 或 alt+c。他们给你的和我给你的之间的代码差异是,即使应用程序最小化,这也能工作。
Public Const MOD_ALT As Integer = &H1 'Alt key
Public Const WM_HOTKEY As Integer = &H312 '
<DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
End Function
<DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Dim id As IntPtr = m.WParam
Select Case (id.ToString)
Case "100"
MessageBox.Show("You pressed ALT+D key combination")
Case "200"
MessageBox.Show("You pressed ALT+C key combination")
End Select
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
UnregisterHotKey(Me.Handle, 100)
UnregisterHotKey(Me.Handle, 200)
End Sub
Private Sub Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
RegisterHotKey(Me.Handle, 100, MOD_ALT, Keys.D)
RegisterHotKey(Me.Handle, 200, MOD_ALT, Keys.C)
end sub