VB.NET中如何制作快捷键

发布于 2025-01-05 07:41:52 字数 298 浏览 7 评论 0原文

我想通过为我的特定程序定义的快捷方式运行特定代码。例如,如果我单击 F1Ctrl+C 那么我希望我的程序显示客户列表。我已经使用以下代码尝试了表单的 keydown 事件

If e.KeyCode = Keys.F1 Then
    Form6.button4.performclick()
End If

但这似乎不起作用。有人能帮我吗?

我在 Visual Studio 2005 中使用 vb.net 来开发我的应用程序

I want to run specific code through shortcuts defined for my specific program. For example if I click F1 or Ctrl+C then I want my program to show customers list. I have tried form's keydown event with following code

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

陈甜 2025-01-12 07:41:52

试试这个:

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

来源

不过,您最好创建自己的事件并引发它而不是引发按钮的单击事件。

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

Source

Although, you'd be better to create your own event and raise that instead of raising the button's click event.

清晰传感 2025-01-12 07:41:52

“访问键是菜单、菜单项或按钮等控件标签文本中带下划线的字符。使用访问键,用户可以通过按 ALT 键与例如,如果按钮运行一个打印表单的过程,因此其 Text 属性设置为“Print”,则在字母“P”之前添加“&”会导致字母“P”在下面划线。用户可以在运行时运行按钮文本。通过按 ALT+P 与按钮关联的命令 对于无法接收焦点的控件,您无法使用访问键。”

<一href="http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CC8QFjAB&url=http://msdn .microsoft.com/en -us/library/bbd5w3b8%28v=vs.85%29.aspx&ei=Or45T6vDCYXhtgeMr5z1Cg& ;usg=AFQjCNFVoyUOFouX2zP2w6IpD8vCywDcbQ&sig2=AoWGhxOgxcVB2zgp16kypQ" rel="nofollow">访问键

尝试访问键吗?

"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."

Access Keys

Try access keys?

酒与心事 2025-01-12 07:41:52

这是一个非常简单的方法来完成类似的事情:

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

不过这是 2010 年的。

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.

唐婉 2025-01-12 07:41:52

我更喜欢通过为快捷方式添加菜单来实现快捷方式,为每个快捷方式添加菜单项,然后将快捷方式绑定到菜单项。这很好,因为它记录了可用的快捷方式并使编码变得非常简单。

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.

甜味拾荒者 2025-01-12 07:41:52

您的代码是正确的,但您应该在表单中设置 keyPreview = true

Your code is correct but you should set keyPreview = true in your Form

伊面 2025-01-12 07:41:52

我看到列出的答案,这已经很旧了,但试试这个。热键设置为 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

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.

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
梦醒灬来后我 2025-01-12 07:41:52

我遇到过同样的情况,我使用了menustrip并指定了快捷键,并使用了performclick功能并将menustrip的属性设置为visible false,它工作得很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文