Jump to content
Sign in to follow this  
BCJensen

In need of OnLeave event ability.....

Recommended Posts

BCJensen

Hello AutoIt Forum! Finally I have something to post here, that I haven't been able to resolve on my own.... eventually. Granted, I'm still learning this AutoIt scripting language so working examples speak volumes to me. Main reason I haven't needed to post a question before now. Love this forum and the examples!!!!

Here's the situation...

Long story short, management wants a windows app that allows a person to fill out an employment application to have less 'blank' fields and enforce a user to enter in something. Also to enforce some office rules they would like in place. ie. If box 18 is checked, have user fill out form XYZ as well. Before anyone speaks up about why.... it's management, their idea/request! :-/

- I've got the user interface created, kept it simple

- I've come up with my own routines (UDF) in the form creation and printing from within AutoIt. *working perfectly*

Took some digging within the forum to piece together a printing solution that worked well with creating forms on the fly.

But my Last issue to resolve is ....

I need to have the ability to have a couple of events,

OnEntry - Logic to be executed once focus is on the field and before the user enters in data into the field, like

pre-filling an employment date. To aid in preventing employment gaps on the application.

OnLeave - Logic (mostly validation) to execute once focus has left the field.

I've been going in circles trying to get this level of 'events' without success. I use the term 'events' ONLY to convey what I'm after.

I've tried using the _IsPressed, (OMG.... can't believe there isn't something that will just return back the lastkey pressed) It presented more problems then it solved. Tried using GUIRegisterMsg, even tried the Opt("GUIOnEventMode", 1) option. <head hitting desk><head hitting desk><head hitting desk>

Now if I knew what field I'm entering or leaving, I could take it from there. (If I could ONLY have one, then it MUST be leaving a field so I can do the requested validations management is asking of me)

If it's not possible..... how about a way to tell what is the current field in focus is and the lastkey pressed, I could even get creative and use that to accomplish the goals of this project.

* additional question - IS there a way to get the lastkey pressed without having to _IsPressed() for every possible key???????

-Barry

Edited by Melba23
Enlarged font

Share this post


Link to post
Share on other sites
PhoenixXL

OnLeave event, this has names of different notifications in the windows control I believe

for which control do you want this for

If you increase the size of your font you may get more help !!

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
PhoenixXL

Here is an example for onblur Even of Edit control,

It sends an EN_KILLFOCUS notification through the WM_COMMAND according to MSDN

#include <EditConstants.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

GUICreate(@ScriptName)
GUICtrlCreateEdit("Edit 1", 10, 10, -1, -1, 0)
Global $hEdit = GUICtrlGetHandle(-1)
GUICtrlCreateEdit("Edit 2", 10, 200, -1, -1, 0)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState()

While GUIGetMsg() <> $GUI_EVENT_CLOSE
Sleep(10)
WEnd


Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
If $lParam <> $hEdit Then Return $GUI_RUNDEFMSG
Local $iNotification = _WinAPI_HiWord($wParam)
Switch $iNotification
Case $EN_KILLFOCUS
ConsoleWrite("Edit1 Blurred" & @CR)
Return 0
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
BCJensen

(First off, sorry about the font. I didn't set anything specific to make it turn out that way)

PhoenixXL, a BIG Thanks for that example!!!!

After some experimentation, I came up with this example that DOES allow both an OnEntry as well as OnLeave events.

Kudos PhoenixXL for your guidance........

#include <EditConstants.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Global Const $OnEntry = 256, $OnLeave = 512
GUICreate(@ScriptName)
Global $edit1 = GUICtrlCreateEdit("Edit 1", 10, 10, -1, -1, 0)
Global $hEdit1 = GUICtrlGetHandle(-1)
Global $edit2 = GUICtrlCreateEdit("Edit 2", 10, 200, -1, -1, 0)
Global $hEdit2 = GUICtrlGetHandle(-1)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState()

While GUIGetMsg() <> $GUI_EVENT_CLOSE
Sleep(10)
WEnd

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
Switch $lParam
Case $hEdit1, $hEdit2
     Local $iHi = _WinAPI_HiWord($wParam)
     Local $iLo = _WinAPI_LoWord($wParam)
Case Else
     Return $GUI_RUNDEFMSG
EndSwitch

if $iHi = $OnLeave Then
Switch $iLo
     Case $edit1
     ConsoleWrite("OnLeave Edit1" & @CRLF)
     Return 0
     Case $edit2
     ConsoleWrite("OnLeave Edit2" & @CRLF)
     Return 0
EndSwitch
EndIf

if $iHi = $OnEntry Then
Switch $iLo
     Case $edit1
     ConsoleWrite("OnEntry Edit1" & @CRLF)
     Return 0
     case $edit2
     ConsoleWrite("OnEntry Edit2" & @CRLF)
     Return 0
EndSwitch
EndIf

Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND
  • Like 1

Share this post


Link to post
Share on other sites
BCJensen

Odd problem.....

Thought that this approach was the answer, but after updating my little app with this logic. Well all works fine until there are 2 blank fields in a row. Upon leaving first blank field I get my "can't have a blank field" msg, I will continually get this message and a message for the next blank field as well. Even though I have NOT gotten that far yet. It continues to popup the message for both fields. If I have 3 blank fields in a row, it still only pops up the error message for the current field and the next, back and forth, back and forth.

For the most part I have not really changed the core of the function, but here it is for review:

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

Switch $lParam
Case $hFirstName, $hMiddleName, $hLastName, $hjob, $hssn, $hStartWork
     Local $iHi = _WinAPI_HiWord($wParam)
     Local $iLo = _WinAPI_LoWord($wParam)
Case Else
     Return $GUI_RUNDEFMSG
EndSwitch

if $iHi = $OnEntry Then Return $GUI_RUNDEFMSG ; ignore all OnEntry msgs

if $iHi = $OnLeave Then
Switch $iLo
     Case $job, $FirstName, $MiddleName, $LastName, $ssn, $StartWork
     if GUICtrlRead($iLo) = "" Then ; generic logic for blank required fields
         msgbox(0,"Error","Mandatory field, entry required." )
         GUICtrlSetState($iLo, $GUI_FOCUS) ; return focus back to field just left
         return 0
     EndIf

EndSwitch
EndIf

Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND

What I don't understand.... is it seems that the focus actually gets set to the next field for some reason. Even though my logic fires the check for a blank field, pops up the error message and then attempts to set focus back to the blank field for the user to actually enter something. How to prevent focus on the next field when I need the user to stay within the current field? I believe the answer to this question should solve this hiccup.

I'm primarily a UNIX programmer for the last 20+ years and the Windows relm is quite a different beast then what I'm use to. I know it's probably something simple, just giving some insight why it's not obvious to me.

Share this post


Link to post
Share on other sites
PhoenixXL

The

msgbox(0,"Error","Mandatory field, entry required." )
might be creating issues. The function should be as fast as possible. Blocking functions like MsgBox should be avoided

Regards :)


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
PhoenixXL

Here is an example for you :)

#include <GUIEdit.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>

Global Const $OnLeave = $EN_KILLFOCUS
GUICreate(@ScriptName)
Global $edit1 = GUICtrlCreateEdit("", 10, 10, -1, -1)
Global $edit2 = GUICtrlCreateEdit("", 10, 200, -1, -1)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState()

While GUIGetMsg() <> $GUI_EVENT_CLOSE
Sleep(10)
WEnd

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
Local $iLo = _WinAPI_LoWord($wParam)
Switch $iLo
Case $edit1, $edit2
Local $iHi = _WinAPI_HiWord($wParam)
Switch $iHi
Case $OnLeave
If GUICtrlRead($iLo) = "" Then
#cs
GUICtrlSetState($iLo, $GUI_FOCUS)
is causing some sort of deadlock in my computer
This works pretty well for me
#ce
_GUICtrlEdit_ShowBalloonTip($lParam, "Error", "Fields cant be blank", $TTI_WARNING)
_WinAPI_SetFocus($lParam)
EndIf
EndSwitch
EndSwitch
#cs
If $iHi = $OnEntry Then
Switch $iLo
Case $edit1
ConsoleWrite("OnEntry Edit1" & @CRLF)
Return 0
Case $edit2
ConsoleWrite("OnEntry Edit2" & @CRLF)
Return 0
EndSwitch
EndIf
#ce
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND
Regards :)

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
BCJensen

PhoenixXL,

<bow to sensei> Awesome example, got your point across perfectly and it DOES solve the weird problem I was having.

I haven't actually used or come across the balloon tip function before. But it's obviously a slick way around the problem and I actually like this method of user feedback better then msgbox(). Looks like I'll be updating some of my previous scripts!

Share this post


Link to post
Share on other sites
PhoenixXL

glad to be of service :)


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×