Return Control to Userform

S

Stephen Newman

I have a userform with the following code attached to an image:

Private Sub Image1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
IsKeyPressed
If Ctrl = True Then
MsgBox "You double clicked the image" & vbNewLine & "while holding the
Ctrl key.", vbInformation, "Control Return Issue"
DoEvents
Else: MsgBox "You double clicked the image.", vbInformation, "Control
Return Issue"
End If
End Sub

In both cases when the code has completed control is not returned to
the userform until the user mouse clicks the form once. There are
additional buttons on the form, but even when I add a setFocus command
following the msgbox, the button is focused, but still needs the extra
mouse click to be operable.

I have tried adding the DoEvents command, but it still does not return
control to the userform.

Am I missing something? If so, could somebody please point out where
I'm wrong?
 
R

RB Smissaert

Have you tried doing SetFocus with one control and then another control?
Another option is to send a mouse-click with the API or use the Putfocus API
on the form.

RBS
 
R

Rumplestiltskin

On Sun, 20 Jan 2008 17:36:48 -0000, "RB Smissaert"

Tried the setfocus on individual buttons, and cycled through them. No
luck. I haven't been successful in discovering a way to send a mouse
click. Tried several different keystrokes thinking I could possibly
use a SendKeys function, but none of the keyboard keys have any
effect.

I searched for putFocus, and didn't see anything I could understand.

Thanks for the ideas.
 
R

RB Smissaert

Try something like this:

Option Explicit
Private Declare Function Putfocus _
Lib "user32" _
Alias "SetFocus" _
(ByVal hwnd As Long) As Long
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Function GetFormHwnd(strCaption As String) As Long
If Val(Application.Version) >= 9 Then
GetFormHwnd = FindWindow("ThunderDFrame", strCaption)
Else
GetFormHwnd = FindWindow("ThunderXFrame", strCaption)
End If
End Function

Sub SetFormFocus(strCaption As String)
Putfocus GetFormHwnd(Me.Caption)
End Sub


RBS
 
S

Stephen Newman

On Sun, 20 Jan 2008 19:13:57 -0000, "RB Smissaert"

I kept searching and found a few references that I was able to
edit/combine and came up with this.

Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN = &H8
Public Const MOUSEEVENTF_RIGHTUP = &H10
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20
Public Const MOUSEEVENTF_MIDDLEUP = &H40
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx
As Long, _
ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As
Long)

Sub LeftClick()
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Sub RightClick()
mouse_event MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0
End Sub

Sub CenterClick()
mouse_event MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0
mouse_event MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0
End Sub

I put the code in a module, called LeftClick after endIf in the
original code, and it did the trick for me. I'm not sure, but from
what I read while combining the codes I found, the four digits
represent co-ordinates of the mouse.

I'm not sure if the Putfocus would have worked or not as I alt/tabbed
through all the open programs I had running, and when I returned to
excel I was faced with the same issue.

I do appreciate your assistance, as your original suggestion did lead
me in the direction of a solution. Thank you. Problem solved.
 
R

RB Smissaert

Mouse-click is fine, but a focus API (there are other ones) is probably the
cleaner solution as a mouse-click does a lot of things that you don't need.
I think PutFocus should do the job, judging from this bit of code:

In a normal module:
----------------------------------

Option Explicit
Private Declare Function Putfocus _
Lib "user32" _
Alias "SetFocus" _
(ByVal hwnd As Long) As Long
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Function GetFormHwnd(strCaption As String) As Long
If Val(Application.Version) >= 9 Then
GetFormHwnd = FindWindow("ThunderDFrame", strCaption)
Else
GetFormHwnd = FindWindow("ThunderXFrame", strCaption)
End If
End Function

Sub SetFormFocus(lHwnd As Long)
Putfocus lHwnd
End Sub

Sub LoadForm()
Load UserForm1
UserForm1.Show 0
End Sub


In a Userform code module:
----------------------------------------

Option Explicit
Private lFormHwnd As Long

Public Property Let propFormHwnd(lHwnd As Long)
lFormHwnd = lHwnd
End Property
Public Property Get propFormHwnd() As Long
propFormHwnd = lFormHwnd
End Property

Private Sub CommandButton1_Click()

Dim bFocusForm As Boolean

If MsgBox("Get the focus back on the form?", _
vbYesNo, _
"hWnd of this form: " & Me.propFormHwnd) = vbYes Then
bFocusForm = True
End If

'focus away from the form, needs to do differently if XL version < 2002
SetFormFocus Application.hwnd

If bFocusForm Then
'focus back to the form
SetFormFocus Me.propFormHwnd
End If

End Sub

Private Sub UserForm_Initialize()
Me.propFormHwnd = GetFormHwnd(Me.Caption)
End Sub


You may not need the form hWnd property, but it can come in handy it other
situations
to have easy access to the hwnd of the form.


RBS
 
S

Stephen Newman

On Sun, 20 Jan 2008 21:06:35 -0000, "RB Smissaert"

I did copy and save this code, as there are other parts of my code
with which I want to direct focus to controls or text boxes. I've had
trouble with this in the past, particularly when I use msgbox. It
seems if I use the OK button, the code executes as expected, and the
focus returns where I want it. If, however, I use the Enter key the
focus doesn't seem to respond to my requests. I'll play with this when
I comprehend it a little better.

Thank you for your help.
 

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