Solved

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

Posted on 2006-11-11
6
179 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

13 Experts available now in Live!

Get 1:1 Help Now