• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 723
  • 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
Lee Faulkner
Asked:
Lee Faulkner
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
 
Lee FaulknerICT Support TechnicianAuthor 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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