Brenmank

Custom Input Mask

7 posts in this topic

I am new to Autoit, and I have created a GUI form that users will enter a project number and Project name into a input box.   The program will then create a directory on our file servers based on the information in the forms.   I want to create an input mask I can control the information entered.

 

Here is how I would like each box's data to look.

 

Project #    12-1234-00.   -  I want the - to always appear in the third and 8 spot.   The user would only have to typew 12123400, and the information would automatically format itself.,

Project Name -  I just need an input to replace any spaces with an underscore.

 

Any help that someone could provide would be great.

 

Thanks...

Share this post


Link to post
Share on other sites



38 minutes ago, Brenmank said:

Project Name -  I just need an input to replace any spaces with an underscore.

...Any help that someone could provide would be great.

StringReplace is the magic func for this:

StringReplace
Replaces substrings in a string.


StringReplace ( "string", "searchstring/start", "replacestring" [, occurrence = 0 [, casesense = 0]] )

Have a look in the helpfile and it's example to understand.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here the input section:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("Test GUI", 581, 43)
$iLable = GUICtrlCreateLabel("Project #", 6, 8, 72, 25)
GUICtrlSetFont(-1, 14, 400, 0, "Times New Roman")
$iInput = GUICtrlCreateInput("", 96, 8, 385, 21)
$iButton = GUICtrlCreateButton("Button", 496, 8, 75, 25)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIRegisterMsg($WM_COMMAND, "")
            GUIDelete()
            Exit
    EndSwitch
WEnd

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) = $iInput Then
        Local $s = StringRegExpReplace(GUICtrlRead($iInput), "[^0-9]", "")
        GUICtrlSetData($iInput, $s)
        Switch StringLen($s)
            Case 3 to 6
                GUICtrlSetData($iInput, StringLeft($s, 2) & "-" & StringMid($s, 3))
            Case 7 To 10
                GUICtrlSetData($iInput, StringLeft($s, 2) & "-" & StringMid($s, 3, 4) & "-" & StringMid($s, 7, 2))
        EndSwitch
    EndIf
    Return "GUI_RUNDEFMSG"
EndFunc

The 2nd part is very easy by following what AutoBert has said.

Edited by UEZ
Fixed issue when non digits were entered.
1 person likes this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks for all the help.  I will give it a try...

Share this post


Link to post
Share on other sites

:)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("Test GUI", 530, 70)
GUICtrlCreateLabel("Project #", 6, 8, 110, 25)
GUICtrlSetFont(-1, 14, 400, 0, "Times New Roman")
$iInput1 = GUICtrlCreateInput("", 120, 8, 385, 21)
GUICtrlSetLimit(-1, 10)
GUICtrlCreateLabel("Project name", 6, 35, 110, 25)
GUICtrlSetFont(-1, 14, 400, 0, "Times New Roman")
$iInput2 = GUICtrlCreateInput("", 120, 35, 385, 21)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIRegisterMsg($WM_COMMAND, "")
            GUIDelete()
            Exit
    EndSwitch
WEnd

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    Switch BitAND($wParam, 0x0000FFFF) 
      Case $iInput1
        Local $s = StringRegExpReplace(GUICtrlRead($iInput1), '\D', "")
        GUICtrlSetData($iInput1, StringRegExpReplace($s, '(^\d{2})|(\d{4})\K', "$1-") )
      Case $iInput2
        GUICtrlSetData($iInput2, StringRegExpReplace(GUICtrlRead($iInput2), '[\h_]+', "_"))
    EndSwitch
    Return "GUI_RUNDEFMSG"
EndFunc

 

1 person likes this

Share this post


Link to post
Share on other sites

Hello Guys,

I´ve a very similar situation here.  There is one inputbox in my GUI which I should receive a Product-ID and I would like to use this very good solution provided by @mikell to avoid problems of mistyping.

My project ID is like this example: 1234567-89.2016.8.19.0001

The biggest problem with my Product-ID is related to the existence of two different types of separators such as "-" and " . " and because of this I´ve no idea to solve this using Regex.... maybe the best approach is StringReplace but once that I my ID is so big, StringReplace is killing me.

So I was wondering if someone here could help me to achieve this goal... 

Cheers

Share this post


Link to post
Share on other sites
2 hours ago, Tormund said:

Hello Guys,

I´ve a very similar situation here.  There is one inputbox in my GUI which I should receive a Product-ID and I would like to use this very good solution provided by @mikell to avoid problems of mistyping.

My project ID is like this example: 1234567-89.2016.8.19.0001

The biggest problem with my Product-ID is related to the existence of two different types of separators such as "-" and " . " and because of this I´ve no idea to solve this using Regex.... maybe the best approach is StringReplace but once that I my ID is so big, StringReplace is killing me.

So I was wondering if someone here could help me to achieve this goal... 

Cheers

 

Thank you guys, I just found a way to achieve what I was looking for. 
I´ll post the code here for future reference.
By the way, I just modified the code for my needs. The original post came from this thread (http://forum.autoitbrasil.com)

 

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
Local $iCode = BitShift($wParam, 16)
Local $iIDFrom = _WinAPI_LoWord($wParam)
Switch $iIDFrom
Case $tbMyInputNumber
Switch $iCode
Case $EN_UPDATE
Local $sWMCOMMAND_MyInputNumberRead = GUICtrlRead($tbMyInputNumber)
Local $sWMCOMMAND_MyInputNumberLastChar = StringRight($sWMCOMMAND_MyInputNumberRead, 1)
Local $aWMCOMMAND_MyInputNumberDotSplit = StringSplit($sWMCOMMAND_MyInputNumberRead, "/")

If StringLen($sWMCOMMAND_MyInputNumberRead) = 7 Then
GUICtrlSetData($tbMyInputNumber, $sWMCOMMAND_MyInputNumberRead & "-")
ElseIf StringLen($sWMCOMMAND_MyInputNumberRead) = 10 Then
GUICtrlSetData($tbMyInputNumber, $sWMCOMMAND_MyInputNumberRead & ".")
ElseIf StringLen($sWMCOMMAND_MyInputNumberRead) = 15 Then
GUICtrlSetData($tbMyInputNumber, $sWMCOMMAND_MyInputNumberRead & ".")
ElseIf StringLen($sWMCOMMAND_MyInputNumberRead) = 17 Then
GUICtrlSetData($tbMyInputNumber, $sWMCOMMAND_MyInputNumberRead & ".")
ElseIf StringLen($sWMCOMMAND_MyInputNumberRead) = 20 Then
GUICtrlSetData($tbMyInputNumber, $sWMCOMMAND_MyInputNumberRead & ".")
ElseIf StringLen($sWMCOMMAND_MyInputNumberRead) = 25 Then
GUICtrlSetData($tbMyInputNumber, $sWMCOMMAND_MyInputNumberRead)
EndIf
Case Else

EndSwitch
EndSwitch

Return $GUI_RUNDEFMSG
EndFunc ;==>WM_COMMAND

 

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