Angelo Júnior BR

Input mask

4 posts in this topic

#1 ·  Posted

Hi

 

I need to create a mask on the control GUICtrlCreateInput to force ".", "()" and "-" between the digits.

The final format would be 000.000.000-00 and the other (00)00000-0000

 

Can you help me

Thsnks!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

How does this example work for you

#include <GUIConstants.au3>
#include <WinApi.au3>
#include <String.au3>

Global $hGUI, $idInput

GUIRegisterMsg($WM_COMMAND, _WM_COMMAND)

_Example()

Func _Example()
    $hGUI = GUICreate("Example", 300, 150)
    $idInput = GUICtrlCreateInput("", 10, 10, 180, 25)
    GUICtrlSetFont(-1, 11)
    GUISetState(@SW_SHOW, $hGUI)

    While 1
        Switch (GUIGetMsg())
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd
EndFunc   ;==>_Example

Func InputFormated()
    Local $s = GUICtrlRead($idInput)
    If Not StringRegExp($s, '^[\d\.\-\(\)]*$') Or StringLen($s) > 14 Then Return GUICtrlSetData($idInput, StringTrimRight(GUICtrlRead($idInput), 1))
    If StringLeft($s, 1) = "(" Then
        If StringLen($s) = 3 Then GUICtrlSetData($idInput, $s & ")")
        If StringLen($s) = 9 Then GUICtrlSetData($idInput, $s & "-")
    Else
        If StringLen($s) = 3 Or StringLen($s) = 7 Then GUICtrlSetData($idInput, $s & ".")
        If StringLen($s) = 11 Then GUICtrlSetData($idInput, $s & "-")
    EndIf
EndFunc   ;==>InputFormated

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If $hWnd = $hGUI And _WinAPI_HiWord($wParam) = $EN_CHANGE And _WinAPI_LoWord($wParam) = $idInput Then InputFormated()
    Return "GUI_RUNDEFMSG"
EndFunc   ;==>_WM_COMMAND

 

Edited by Deye
1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted

Woooow, very good!! Thank you very much Deye!!


Is there any way I can adapt this same code to make the mask "(00)00000-0000" or do I need another one?

 

Thanks again!

Share this post


Link to post
Share on other sites

#4 ·  Posted

you are welcome

Changes in the above example  !
Now, if your first typed character is "(" it will autocomplete to "(00)00000-0000" 
else it will autocomplete to "000.000.000-00"

1 person likes this

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