Positioning Cursor

G

Guest

hi to everybody,

is there any way, to determine the position of the cursor in x and y
coordinates by using visual basic?

E. g. when calling a ComboBox, I would like to have the cursor on a distinct
position (in fact out of the way, which means at height of the menubar).

Ulrich 1947
 
J

John Spencer

Do you mean you want to set the position of the cursor? For example, force
the cursor position to screen coordinates 250, 300 when a specific event
occurs? If so, I don't believe it can be done with VBA. Of course, I'm
probably wrong and Stephen Lebans or one of the other "alpha geeks" will
tell us both how to accomplish this.

My question is why you think you want to interfere with the user's
positioning of the cursor. I would generally be upset to see the cursor
position change outside of my control. Can you explain the need to do this?
There might be a better solution to accomplish your goal.
 
D

Dirk Goldgar

Ulrich1947 said:
hi to everybody,

is there any way, to determine the position of the cursor in x and y
coordinates by using visual basic?

E. g. when calling a ComboBox, I would like to have the cursor on a
distinct position (in fact out of the way, which means at height of
the menubar).

Ulrich 1947

There are Windows API functions GetCursorPos and SetCursorPos, but to
use them within Access with respect to Access forms and controls, I
think you're also going to have to make a translation between Access's
coordinate system and the Windows screen coordinates. I wouldn't be
surprised if someone like Stephen Lebans has figured this out, so you
may want to poke around at www.lebans.com , as well as searching the
web, before setting out to write your own translator function.
 
G

Guest

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.

Thanks

Ulrich1947
 
D

Dirk Goldgar

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top