Faalamva Posted June 29, 2014 Posted June 29, 2014 Hi, I know how to read an Edit control, check and process its content accordingly. But what I'm trying to achieve today is format the input of the control as soon as its content is validated (by getting the focus out of the Edit control). Let me explain with a few examples : 1) At first, my edit zone is empty. Enter the price : [ ] 2) The user inputs some correct value, then clicks another control OR presses ENTER. Enter the price : [ 3 ] I want the post-processing to do this as soon as the user selects another control : Enter the price : [ 3.00 € ] 3) The user inputs some incorrect value, then clicks another control OR presses ENTER. Enter the price : [ x ] I want the post-processing to display a MsgBox "Incorrect value" and put the focus back the the control Any idea how to automatically trigger this post-processing ? Thank you ! Faalamva
Solution Terenz Posted June 29, 2014 Solution Posted June 29, 2014 Try this example made by me expandcollapse popup#include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> Global $fFlag = True Local $hForm = GUICreate("Form", 247, 143, 236, 244) Local $sInput1 = GUICtrlCreateInput("0", 96, 56, 121, 21, BitOR($ES_CENTER, $ES_NUMBER)) Local $sInput2 = GUICtrlCreateInput("Another one", 96, 100, 121, 21) Local $sLabel = GUICtrlCreateLabel("Enter the price", 16, 56) Local $sEnter = GUICtrlCreateDummy() Local $AccelKeys[1][2] = [["{ENTER}", $sEnter]] GUISetAccelerators($AccelKeys) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $sEnter _MyFunc() EndSwitch Switch _GUICtrlGetFocus($hForm, $sInput1) Case 1 If $fFlag Then _MyFunc() $fFlag = False EndIf Case 0 If Not $fFlag Then $fFlag = True EndSwitch WEnd Func _MyFunc() GUICtrlSetData($sInput1, StringFormat("%.2f", GUICtrlRead($sInput1))) If Not StringInStr(GUICtrlRead($sInput1), "€") Then GUICtrlSetData($sInput1, GUICtrlRead($sInput1) & " €") EndFunc ;==>_MyFunc Func _GUICtrlGetFocus($hGUI, $sControl) Local $hHwn = ControlGetHandle($hGUI, "", ControlGetFocus($hGUI)) If $hHwn <> ControlGetHandle($hGUI, "", $sControl) Then Return 1 ; lose focus If $hHwn = ControlGetHandle($hGUI, "", $sControl) Then Return 0 ; has focus EndFunc ;==>_GUICtrlGetFocus Nothing is so strong as gentleness. Nothing is so gentle as real strength
Faalamva Posted June 29, 2014 Author Posted June 29, 2014 (edited) @Terenz : tested and looks pretty good, thank you for your quick answer and also for providing a full-code example. Edited June 29, 2014 by Faalamva
Terenz Posted June 29, 2014 Posted June 29, 2014 (edited) Polymath? The nick is above Polymath, Mr.Seeker. If the problem is solved click on "Mark Solved" button Edited June 29, 2014 by Terenz Nothing is so strong as gentleness. Nothing is so gentle as real strength
Faalamva Posted June 29, 2014 Author Posted June 29, 2014 Now that's funny, sorry for the mistake (edited by the way) Topic marked as solved, thank you !
Faalamva Posted June 29, 2014 Author Posted June 29, 2014 OK, I managed to understand most of the code, but I still have a few questions : 1) Why are you using GUICtrlCreateInput over GUICtrlCreateEdit ? I guess it is more appropriate, but could you tell me why ? 2) I never really used accelerators/hotkeys until now, but I've read some documentation about HotKeySet. Why are you using an accelerator, wouldn't HotKeySet("key"[,"function"]) be able to link directly the ENTER key to _MyFunc ? 3) The $ES_NUMBER input style is pretty clever, but I'd like to input floating numbers as well (3.25 for example). Is there a similar "short" way to do it (involving a style) ? Thank you !
Terenz Posted June 29, 2014 Posted June 29, 2014 (edited) 1) No particular reason 2) Accelerator are more appropriare for native autoit GUI and work only in that window, HotKeySet work globally so if you press ENTER with notepad opened-focused and your script launched it enable MyFunc() 3) No, other way involving WM_COMMAND but is a little "advance" for you. Remove $ES_NUMBER and check if the string is valid with function like StringIsFloat Edited June 29, 2014 by Terenz Nothing is so strong as gentleness. Nothing is so gentle as real strength
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now