PDA

View Full Version : [SOLVED:] UserForm loses focus after an Application.InputBox



Zars01
05-30-2017, 06:28 PM
Hi! Here's the tldr; - my topmost UserForm loses focus after an Application.InputBox grabs user input.

I have a macro that pulls open a lot of UserForms. They're all ShowModal = True.

The UserForm with the (mild) issue is called from UserForm 2 or 3, depending on user interaction.
It adds labels depending on the underlying Excel sheet and uses a class to give each label properties, DblClick, MouseDown, MouseMove, MouseUp events, etc.

Everything works fine and I'm very happy with it - except ...

The DblClick event calls "Application.InputBox" - after, the user must click the UserForm to reapply focus ...
As far as code, after Application.InputBox is called -
User input is verified, stored, and used to change the label's Left.
A function is called that reads EVERY label's left, and updates a few key control tags and colors, etc.

This is a very small issue as it doesn't really interfere with functionality.
At worst, it just feels like the computer is being slow, etc. when you have to double click to press a button (once to activate the UserForm, once to call the event).

Still, it's a puzzle ...

Here's what I've tried so far:
Attempts that didn't compile:
Setting focus on the label (because labels don't take focus)
UserForm.Show (already shown, Modal)

Setting focus on a command button on the UserForm (this APPEARS to work, the UserForm's gray title bar becomes blue - but user must still click on UserForm to interact with it)
Using AppActivate to activate Excel (no impact)

I have stepped through, of course. No insight gained.

I'm using Microsoft Excel 2013.

Any ideas?

Paul_Hossler
05-30-2017, 07:27 PM
The DblClick event calls "Application.InputBox" - after, the user must click the UserForm to reapply focus ...



Guessing here ...

Not real code




Sub DoubleClick

s = Application.InputBox

...do things ....


userform1.repaint (???)

userfrom1.commandbutton1.Setfocus (???)

SamT
05-30-2017, 10:10 PM
Possibly

Option Explicit
Dim CrntFrm As Object


Sub DblClick
Set CrntFrm = UserForm1

s = Application.InputBox

Blah Blah

CrtFrm.Function1
Set CrntFfm = Nothing
End Sub

GTO
05-31-2017, 01:54 AM
Hi there,

Granted a quick try, but I could not replicate. The Modal form's title bar stays grey on dismissing the Input Box, but one click on a button was effective. So... I'm with Sam and Paul, a guess. Does seem to reliably return focus.



Option Explicit

#If VBA7 Then
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private hWndMe As LongPtr
#Else
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private hWndMe As Long
#End If

Private Sub CommandButton1_Click()
If UCase$(TypeName(ActiveSheet)) = "WORKSHEET" Then
Cells(1).Value = Application.InputBox("ENter a number", vbNullString, 123, Type:=1&)
DoEvents
Call SetForegroundWindow(hWndMe)
End If
End Sub

Private Sub CommandButton2_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim sCaption As String

Randomize
sCaption = Me.Caption
Me.Caption = CStr(Rnd)
hWndMe = FindWindow("ThunderDFrame", Me.Caption)
Me.Caption = sCaption
End Sub


Hope that helps,

Mark

Zars01
06-06-2017, 12:34 PM
Paul
I just tried adding .Repaint - it didn't change anything. Thanks though!

Mark
It may be a matter of having so many other Userforms open -
Could be using a class to add events to run-time created control.
Maybe it has to do with a double-click event AND a MouseDown, MouseMove and MouseUp?
(Probably all three)

I appreciate the help - looks like there isn't an easy answer.
(I'd post my workbook, but some of the data is proprietary - and there are thousands of lines of code besides.)

I'll post here if I ever figure it out.
Again, thanks for your time

SamT
06-06-2017, 01:26 PM
my topmost UserForm loses focus after an Application.InputBox grabs user input.

I am forced to assume that the InputBox is called from the top UF.

I would try making a Userform that duplicates a standard InputBox with additional code to reshow the desired UF and Set the focus one of its controls.

For example: UF1 code

MyinputBox.Load
MyinputBox.LastForm = ME 'LastForm being a Property Set Sub
MyinputBox.show
Do Events
'Deal with the UserInput variable 'See below

MyinputBox Code:

'Get the Input

with LastForm
.UserInput = The UserInput 'Where UserInput is a Property Let on the UserForm(s)
'Alternately, you may be able to pass the desired control to MyinputBox with another Property Set
.Show
.Controls(1).SetFocus
end with

SamT
06-08-2017, 07:52 PM
Just surfing my Favorites/Bookmarks today I came across this

Bring a Window to Top: http://allapi.mentalis.org/vbtutor/iapi3.shtml

Zars01
06-10-2017, 02:56 PM
I figured it out!

Thanks for working on this with me SamT -
The InputBox is summoned from an event within a class object, declared on the topmost UserForm.

I created my own mimic InputBox and ... no change. I had pretty high hopes for your idea -
The Windows API thing didn't help either

They were super helpful ideas though, because they stripped away a few of my hypotheses. It HAD to be about mouse events

So, here's the solve:

Add "Cancel = True" to the double click event in question!
Apparently mouse event orders are a little complicated, like KeyUp, KeyDown and KeyPress?
Stepping through I could tell that Double Click was the last to fire ...
Cancelling that last event before launching a UserForm OR an InputBox (I've tested both now) ... with or without the "BringToTop" Windows API trick (also tested) ... sidesteps whatever issue it was I was facing.

Thank you all so much for your time on this one. I use forums like these A LOT for answers and I'm glad I finally had a question I could work out with wizards like you

SamT
06-10-2017, 02:58 PM
:thumb: