Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Keypress for any control on form

Posted on 2011-10-24
Medium Priority
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 78

Accepted Solution

slightwv (䄆 Netminder) earned 2000 total points
ID: 37020015
You should be able to intercept this at the form level:
LVL 40
ID: 37020089
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

571 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