is there a way of identifying which control is being pointed at?

CJ_London

Super Moderator
Staff member
Local time
Today, 12:33
Joined
Feb 19, 2013
Messages
17,558
I'm familiar with screen.activecontrol but what I am trying to do is to activate an event relevant to the control currently triggering the mousemove event.

In the attached db, if you open the MMHighlight form and move the mouse over the boxes, as they are moved over they go yellow (mousemove event), whilst still hovering, click on a box and the first box displays the first and last boxes selected (mousedown event). release the click to trigger the mouse up event which clears the boxes. Broadly this is working as intended

The problem is the mousemove event which has to be 'personalised' for each control - highlighted in yellow
attachment.php


In the real application there are several hundred controls, so I'm looking for a timesaver - one option being to build a form via vba, but ideally some equivalent to screen.activecontrol and perhaps a third where I can replace [text0] with a generic name - a bit like 'Me' to refer to the form.

Can anyone suggest an neat alternative?
 

Attachments

  • MMTest.mdb
    MMTest.mdb
    384 KB · Views: 105
  • Capture.JPG
    Capture.JPG
    13.7 KB · Views: 327
Use Screen.ActiveControl inside the function rather than as a parameter.

You could still use the parameter to override the ActiveControl in the function if you want. Just make it an optional argument and build the logic accordingly.

IsMissing() is one way to test for the parameter being passed but this requires a Variant argument.
 
Thanks both

Screen.activecontrol only works if the control has the focus which means the 'swipe' doesn't work if another control has the focus - basically that is the control that is acted on, not the one the mouse is over.

And Allen Brownes suggestion is basically set up the way I have it at the moment in the way I call the function, but also works on the control having the focus, but I will investigate more.

I've been thinking a bit more about how access/windows 'knows' where the controls are in relation to the mouse pointer, so 'knows' which control is under the mouse and therefore which control event to trigger when the mouse is clicked for example. What I imagine (which may not be correct) is that the system maps out the form in memory with the coordinates and dimensions of each control and then compares these with the mouse coordinates to determine which control the mouse is over.

Access to this map on the mousemove event would do what I want...

I guess I could always build my own map..
 
You can create a class that represents a control and any events and methods you wish to associate with that class (control). The result would be an object that you can identify by its name and also directly access it's events that will do stuff based on it's own name (since each object will know the name of the control it is housing). In order to do this you do need to ensure that each event for each control has been set to "[Event Procedure]".

I attach your example altered to make use of this. I've use txtBox rather then more general control but it could be expanded.

I've left you code as is where possible but you could incorporate your code into the class e.g. you can set the colour of a textbox in the class (since it is in fact just a reference to the textbox).

This technique becomes quite cool once you realise you can do all the manipulation via the collection of objects. It doesn't even have to be a collection - could just as easily be an array of objects.

hth
Chris
 

Attachments

I'm kind of hazy on your description of what you're trying to do.

From what I've taken to understand: you're trying to handle the mousemove event on hundreds of controls without adding it to their event handler manually.

If you're adding it to every single control within a container or within a form then maybe this idea/method will work:

in the onload
Code:
Private Sub Form_Load()
    
    Dim control As Variant
    
    For Each control In Me.Controls ' Which ever container.
        control.OnMouseMove = "=CustomFunction([" + control.Name + "])"
    Next
        
End Sub

something like that?
 
I also know some form generating stuff that can load your form into an instance of a form edit these methods and save it as a mutated version for backup purposes.
 
I use what BlueIshDan suggested.
Not sure what your trying to do when the mouse move over controls. You might need to set the same event for the container to close what ever you did.
 
Using stopher's code, the class itself can enable event handling for its own TextBox with this amendment . . .
Code:
[COLOR="Green"]'accessor methods[/COLOR]
Public Property Set txtBox(ByVal oTextBox As TextBox)
    Set m_tb = oTextBox
    With m_tb
        .OnMouseMove = "[Event Procedure]"
        .OnMouseUp = "[Event Procedure]"
        .OnMouseDown = "[Event Procedure]"
[COLOR="Green"]        'do this for whatever events you want to handle[/COLOR]
    End With    
End Property
. . . so anytime you set the control in the class, the control is sure to raise the events you want.
 
Using stopher's code, the class itself can enable event handling for its own TextBox with this amendment . . .
Code:
[COLOR="Green"]'accessor methods[/COLOR]
Public Property Set txtBox(ByVal oTextBox As TextBox)
    Set m_tb = oTextBox
    With m_tb
        .OnMouseMove = "[Event Procedure]"
        .OnMouseUp = "[Event Procedure]"
        .OnMouseDown = "[Event Procedure]"
[COLOR="Green"]        'do this for whatever events you want to handle[/COLOR]
    End With    
End Property
. . . so anytime you set the control in the class, the control is sure to raise the events you want.
ha that's cool. Kinda obvious now you mention it but I wouldn't have thought of doing it.
 
stopher, Markk - pretty much there although in vba the same effect can be achieved without using the class module, as suggested by BluIshDan. Both require an initialisation in the form load event or at some point before the functionality is required.

I suspect that I won't find exactly what I'm looking for but both of these suggestions are a big step forward from where I was so I thank you all!
 
CJ as a said before, if you're going to use the method I have displayed above:

Unless there are different controls being generated with each load, you should only run the code once and save the mutated instance of your form.

Running the code on every load isn't exactly the best way to do it if it is doing the same process every time.
 

Users who are viewing this thread

Back
Top Bottom