Ulrich1947 said:
Hi, John
Yes, i do mean do set the position of the cursor. The MouseMove Event
does not help, as i do get the coordinates, but I want to set them.
The reason is as follows:
On a Form I have a Control, positioned let's say at 4 cm from the
top. When I Click on that control, a ListBox or ComboListBox opens,
positioned let's say at 0 cm from the top filling nearly the whole
screen. At this stage my cursor is on position 4 cm and therefore
marks the 6th, 7th or 8th entry in the list although no entry is
selected. I do not want that, as it might seduce unused user to
assume, this entry would be selected and so hits enter to finish.
Furthermore, if I do not move the cursor manually but select by
arrowup- or arrowdown button any of the entries, the entry marked
will stay on position 4 cm.
Maybe this could be solved by setting focus on another control, but
then I have to put focus back again in order to select an entry and
the thing starts again. I want the cursor have repositioned, e.g. at
the very top or bottom or any corner of the form, from where I can
start moving him again. So maybe Dirk Goldgar's suggestion using
commands like "SetCursorPos" might help, as I do not need a certain
position but any position out of the way (as said top, bottom or
corner), but I wouldn't know how to apply such command.
To make this clear, I know that in fact nothing is selected and how to
select an entry, but for merely optical reasons I want to minimize the
possibility for other users (I collect recipes and the materials they
consist of for people less familiar using such application) to be
irritated or mislead.
I'd be much obliged, if someone would look into this matter and maybe
can make a suggestion.
All right, how about this:
'----- start of module code -----
Option Compare Database
Option Explicit
Private Type POINT
x As Long
y As Long
End Type
Private Declare Sub ClientToScreen Lib "user32" _
(ByVal hWnd As Long, lpPoint As POINT)
Private Declare Function GetFocus Lib "user32" () As Long
Declare Function SetCursorPos Lib "user32" _
(ByVal x As Long, ByVal y As Long) As Long
Public Function MouseToControl( _
ctl As Access.Control, _
Optional LeaveFocus As Boolean = False)
' Move the mouse cursor to a specific control, passed
' as argument "ctl". By default, the focus will be set to
' that control and left there; however, this behavior can
' be controlled by the optional argument "LeaveFocus".
' If LeaveFocus is True, the focus will be returned to
' the control that originally had it.
'
' Note that <ctl> must be a control that is capable of
' receiving the focus. It *will* receive the focus, at
' least momentarily, even if the focus is then set back
' to the previously active control. However, if <ctl>
' cannot receive the focus -- for example, if it's a
' label control -- then the cursor will move to the top
' left corner of the form.
Dim ctlOld As Access.Control
Dim hWnd As Long
Dim pt As POINT
On Error Resume Next
If LeaveFocus Then Set ctlOld = Screen.ActiveControl
ctl.SetFocus
hWnd = GetFocus
ClientToScreen hWnd, pt
SetCursorPos pt.x, pt.y
If LeaveFocus Then
If Not ctlOld Is Nothing Then
ctlOld.SetFocus
End If
End If
End Function
'----- end of module code -----
With the above code in a standard module, you can move the mouse cursor
to the top left position of a given control -- provided it's a control
that can receive the focus -- by executing a statement of the form
MouseToControl Me!ControlName
(substituting the name of the control for "ControlName").
If you want to move the cursor but leave the focus where it is, use a
statement like
MouseToControl Me!ControlName, True
Technically, that doesn't *leave* the focus on the original control;
instead, it *returns* the focus to the original control. That might
cause various GotFocus, LostFocus, Enter, and Exit events to fire, but I
don't see any way around it.