SetFocus appears to be intermittent.

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
PaulSR
Pro Scripter
Posts: 65
Joined: Mon Aug 05, 2013 2:58 pm
Location: Edinburgh/Peterborough

SetFocus appears to be intermittent.

Post by PaulSR » Wed Nov 13, 2013 2:50 pm

Hi,

I've been writing something which has to cater for four individual windows each running the same software. I've added a dialog to my script which asks the user to select a "brand" which I then use to call SetFocus using the dialog result as the window handle. This doesn't seem to be working consistently and now I'm wondering if it ever worked when I only had it coded for a single window (I probably always had the focus on the window to start with).

I run the dialog and capture the return variable :

Code: Select all

Show>Dialog1,modalResult
GetDialogProperty>Dialog1,MSComboBox1,Text,BrandWindow
I then attempt to use that variable throughout the code to ensure I have focus on the correct window :

Code: Select all

SetFocus>BrandWindow
However when I debug this the window does not take precedent onscreen and MS appears to simply stay on the last highlighted window.

Thanks in advance for any help guys.

PaulSR
Pro Scripter
Posts: 65
Joined: Mon Aug 05, 2013 2:58 pm
Location: Edinburgh/Peterborough

Post by PaulSR » Thu Nov 14, 2013 9:23 am

Been having a play - I can easily construct something that jumps from window to window so setfocus isn't the problem - I can also assign the name of a window to a variable before using it with setfocus and that works i.e

Code: Select all

Let>WindowHandle=Iwanttoseethiswindow
SetFocus>WindowHandle
That works fine - and if I debug I can see that my dialog box returns the selected value to the variable - it's beyond this point when I use setfocus that the window doesn't take precedent.

Stumped.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Nov 14, 2013 9:38 am

There is absolutely no issue with SetFocus that I know about. Very important and fundamental function so we'd know if there was.

Without seeing your code and being able to test it I am reduced to guessing. So here are some guesses:

1. The SetFocus is finding the edit box on the dialog because it contains the same text. Try setting focus type to 0 to exclude child windows. Or 2 for visible windows only: Let>WF_TYPE=2

2. There might be a space or some other hidden character in the value coming back from the dialog?

If no such window matches on the system you'd get an error message. If you are not getting an error message it *IS* finding a match. So if the window you want to focus is not the one getting focused and you are not getting an error message then the script must be seeing some other window first.

I suspect it is the former - it is probably finding the combo box first. This works for me:

Code: Select all

Show>Dialog1,r
GetDialogProperty>Dialog1,MSComboBox1,Text,BrandWindow
Let>WF_TYPE=2
SetFocus>BrandWindow
Wait>2
I tested with "Untitled - Notepad" as an option in the combo box and had Notepad open at the time.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

PaulSR
Pro Scripter
Posts: 65
Joined: Mon Aug 05, 2013 2:58 pm
Location: Edinburgh/Peterborough

Post by PaulSR » Thu Nov 14, 2013 10:20 am

Marcus,

Thanks for this information - you were spot on with your observation it was indeed the former and the addition of Let>WF_TYPE=2 appears to have resolved the issue.

Thanks a lot !

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts