Jump to content

Edit control post-processing to check/format the input ?


Go to solution Solved by Terenz,

Recommended Posts

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

Link to comment
Share on other sites

  • Solution

Try this example made by me

#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

 

Link to comment
Share on other sites

Polymath?  :D

The nick is above Polymath, Mr.Seeker. If the problem is solved click on "Mark Solved" button

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

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 ! :bike:

Link to comment
Share on other sites

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 by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...