• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • Last Modified:

Assign an F key to a button

Hi Guys,

I want to assign some buttons on a Windows form to some F keys i.e. F1, F2, F3 etc can anybody shed any light on how to do this?

Lee
0
lfaulkneruk
Asked:
lfaulkneruk
1 Solution
 
rettiseertCommented:
Hi

Set the form¿s property KeyPreview = True and the use some code like this:

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        Select Case e.KeyCode
            Case Keys.F1
                MessageBox.Show("F1 was pressed")
            Case Keys.F5
                MessageBox.Show("F5 was pressed")
            Case Keys.F10
                MessageBox.Show("F10 was pressed")
        End Select

    End Sub
0
 
lfaulknerukAuthor Commented:
Hi rettiseert,

That works great, how can I modify that to do the same as pressing the button e.g.....

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        Select Case e.KeyCode
            Case Keys.F1
                Me.Button2_Click
        End Select

    End Sub

I know that wouldnt work but how can I format that to make it do this?

Lee
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I would override ProcessCmdKey instead:
Public Class Form1

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.F1
                Button1.PerformClick()

            Case Keys.F2
                Button2.PerformClick()

            Case Keys.F3
                Button3.PerformClick()

        End Select

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show("Button1")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MessageBox.Show("Button2")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        MessageBox.Show("Button3")
    End Sub

End Class

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now