Userform default button

DougRobertson

Active Member
Joined
Sep 22, 2009
Messages
334
Office Version
  1. 365
Platform
  1. Windows
When using the vbYesNoCancel Message Box in Excel, you use the code vbDefaultButton1 to have the mouse pointer go to Button 1 when the box is activated.

When creating a UserForm, is there a way to have the mouse pointer go to CommandButton1 when the that box is activated?

Thankyou,

~ Doug
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you set the TabIndex property of that button to 0, that button will have the focus when the userform is created. But it will not move the mousepointer, if you want to do that, you would need some API calls I think...
 
Upvote 0
If you want to run a particular button's code when enter is pressed set it's Default property to True/Yes.
 
Upvote 0
Actually, I was looking for what is sometimes called the Snap To function in mouse parlance.

Any ideas?

~ Doug
 
Upvote 0
You should be able to do that with the SetCursorPos API

Public Declare Function SetCursorPos Lib "user32.dll" (ByVal X As Long, ByVal Y As Long) As Long

Although, as a user, I'd like to point out that I would be absolutely livid if some software started to take over my cursor.

:)
 
Upvote 0
Thanks BCW,

Looks impressive, but I don't have a clue where to place it! And do I use the whole line?

Public Declare Function SetCursorPos Lib "user32.dll" (ByVal X As Long, ByVal Y As Long) As Long


~ Doug
 
Upvote 0
Thanks BCW,

Looks impressive, but I don't have a clue where to place it! And do I use the whole line?

Public Declare Function SetCursorPos Lib "user32.dll" (ByVal X As Long, ByVal Y As Long) As Long


~ Doug

Using the SetCusorPos API is the way to go but there is still the problem of converting the UserForm Button measurements from points to pixels.
 
Upvote 0
Thanks JT,

It's starting to look a little too complicated though.

Time to switch to Manual!

~ Doug
 
Upvote 0
Here is a workbook demo that shows how you can choose any control on a userform and automatically move the mouse cursor over it upon activating the userform.

This simple call will place the mouse cursor right on the middle of CommandButton1 : (Change the target control as needed)

Code:
Call MoveCursorToControl(CommandButton1)
Anyway, here is the whole project code for future reference :

1- In a Standard module :

Code:
Option Explicit

Private Type POINTAPI
  x As Long
  y As Long
End Type

Private Declare Function FindWindow Lib "user32.dll" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function ClientToScreen Lib "user32.dll" _
(ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long

Private Declare Function SetCursorPos Lib "user32.dll" ( _
  ByVal x As Long, ByVal y As Long) As Long
  
Private Declare Function GetDeviceCaps Lib "gdi32" ( _
  ByVal hDC As Long, ByVal nIndex As Long) As Long
  
Private Declare Function GetDC Lib "user32" ( _
  ByVal hwnd As Long) As Long
  
Private Declare Function ReleaseDC Lib "user32" ( _
  ByVal hwnd As Long, ByVal hDC As Long) As Long
  

Private Const LOGPIXELSX As Long = 88
Private Const LOGPIXELSY As Long = 90
Private Const PointsPerInch = 72


Public Sub MoveCursorToControl(ByVal Ctl As Control)

    Dim tPt As POINTAPI
    Dim lhwnd As Long
    
    lhwnd = FindWindow(vbNullString, Ctl.Parent.Caption)
    With Ctl
        tPt.x = PTtoPX((.Left + (.Width / 2)) * Ctl.Parent.Zoom / 100, 0)
        tPt.y = PTtoPX((.Top + (.Height / 2)) * Ctl.Parent.Zoom / 100, 1)
    End With
    ClientToScreen lhwnd, tPt
    SetCursorPos tPt.x, tPt.y

End Sub

Private Function ScreenDPI(bVert As Boolean) As Long
 
   Static lDPI(1), lDC
   
   If lDPI(0) = 0 Then
        lDC = GetDC(0)
        lDPI(0) = GetDeviceCaps(lDC, LOGPIXELSX)
        lDPI(1) = GetDeviceCaps(lDC, LOGPIXELSY)
        lDC = ReleaseDC(0, lDC)
    End If
    ScreenDPI = lDPI(Abs(bVert))
 
End Function
 
Private Function PTtoPX _
(Points As Single, bVert As Boolean) As Long
 
    PTtoPX = Points * ScreenDPI(bVert) / PointsPerInch
 
End Function
2- In the UserForm Module :

Code:
Option Explicit

Private Sub UserForm_Activate()

   Call MoveCursorToControl(CommandButton1)

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,848
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top