Keypress for any control on form

Posted on 2011-10-24
Last Modified: 2012-05-12
I am using visual basic 2010 and I am trying to determine if there is any activity on a form (keypresses, mourseclicks, etc).  Is there any way to do this besides adding a keypress and mouseclick event on every control?
Question by:vollmesg
    LVL 76

    Accepted Solution

    You should be able to intercept this at the form level:
    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    For the KeyPress, you can set the KeyPreview property of the Form to True, and you will be able to grab all the key presses in the Form KeyPress event before they go to the individual controls.

    For the other events, you can do the following.

    Create the event procedure for one of the controls.

    Remove the Handles clause at the end of the method declaration.

    Add something like the following code at the end of the Form_Load event:
    For Each ctl As Control In Me.Controls
        AddHandler ctl.<EventName>, AddressOf <NameOfTheEventProcedure>

    Open in new window

    Here is one for the MouseClick:
    For Each ctl As Control In Me.Controls
        AddHandler ctl.MouseClick, AddressOf TextBox1_MouseClick

    Open in new window

    Inside of the event, if you need to reference the control that triggerend the event, you can do the following with the sender parameter:
    Private Sub TextBox1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
        Dim callingControl As Control
        callingControl = DirectCast(sender, Control)
        MessageBox.Show("This has been called by " & callingControl.Name)
    End Sub

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    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…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now