Solved

Respond to ANY mouse event ANYWHERE on a form with ONE sub routine

Posted on 2006-11-11
6
180 Views
Last Modified: 2010-04-23
I need to respond to a mouse click anywhere on a form.  I'm incorporating this into about 25 forms.  I would like to create a sub routine that allows me to just paste into each one of them without having to customize for each form based on particulars of each form.

I've tried me.mousedown, me.click, mybase.click, mybase.mousedown

I have several group boxes on the form and I get no result when clicking anywhere inside the group boxes, but if I click outside of the group boxes mybase and me.click or .mousedown execute.  Also, any of these forms may have text boxes, combo boxes, listviews, etc.

Is there ONE event that captures for any and all mouse activity throughout a form?


Phil Tate
0
Comment
Question by:TSFLLC
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
newyuppie earned 250 total points
ID: 17922965
i think the only option you have is to add a loop to cycle through every control you want to handle the mouse click event, and wire then via AddHandlers all to 1 particular sub

something in the ballpark of:

for each ctl as control in me.controls
  addhandler ctl.mousedown, address of AllMouseDown
next ctl

in the Load event. and an additional sub

Private Sub AllMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
  'do stuff
End Sub
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17923557
I think there may be an API that might help you to do this, but APIs are not my forte.  

In VB.NET, I would agree with newyuppie, only adding that you will need to make your code recursive because you have textboxes (etc.) within controls (e.g. groupboxes).  Something like this (not tested).

Private Sub AddMouseHandler (ByVal ctl As Control)
      If ctl.HasChildren Then
         AddMouseHandler(ctl)
      End If
      addhandler ctl.mousedown, address of AllMouseDown
Exit Sub

called in Load event like this

for each ctl as control in me.controls
  AddMouseHandler(ctl)
next ctl

Roger
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 250 total points
ID: 17925321
Oops.  I shouldn't code onto the screen at such a (UK) late hour ;-(

This

Private Sub AddMouseHandler (ByVal ctl As Control)
      If ctl.HasChildren Then
         AddMouseHandler(ctl)
      End If
      addhandler ctl.mousedown, address of AllMouseDown
Exit Sub

should be this

Private Sub AddMouseHandler (ByVal ctl As Control)
      If ctl.HasChildren Then
         For Each subCtl As Control In ctl.Controls
            AddMouseHandler(subCtl)
         Next
      End If
      addhandler ctl.mousedown, address of AllMouseDown
Exit Sub

Roger
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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