Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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