Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

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