Solved

Capturing F keystroke inside a form in VB.NET

Posted on 2011-09-13
4
440 Views
Last Modified: 2012-05-12
Hi,

Is it possible to capture, for example, F5 keystroke on a form even if the focus is on a textbox?

I need to capture F5 on a form independently of the control that has the focus.

Thanks!
0
Comment
Question by:pvg1975
4 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 400 total points
ID: 36530936
The simple way to do this is using form.keypreview.  Setting this property true sends all key presses to the form level  handler:
Form.KeyPreview Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
    End Sub
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.F5 Then
            MsgBox("F5")
            'If this is false, then keypress is sent on to focus control
            e.Handled = True
        End If


    End Sub


End Class

Open in new window

0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 100 total points
ID: 36530949
Enable the KeyPreview property of the form and capture the keypress in the KeyDown event of the form
0
 

Author Closing Comment

by:pvg1975
ID: 36530971
I was missing keypreview.

Thanks guys!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36531014
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET return row from SQL to DataSet and get column values ? 3 99
vb.net class 3 21
Error message when using PrintSocket.Send 7 18
Passing data between Forms 3 20
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question