Sign in to follow this  
Followers 0
brainstorm

Help with input fields

10 posts in this topic

#1 ·  Posted (edited)

Hi all...

I've been trying to find if it's possible to duplicate something that can be achieved with VBA. I have a bit of VBA code that asks the user for some information. One of the fields is a numeric field that asks for their phone number. As they start to type it in the field updates automatically to include "()" and "-". That is when the user starts typing in their number e.g (123) 555-6666, as soon as they hit 1, the input field updates to show the starting "(" for the area code. The same goes for the ")" after they type in "5" and so on. The field is a numeric only field so it will not accept "()" or "-". Is there a way to duplicate this in autoit script?

Edited by brainstorm

Share this post


Link to post
Share on other sites



Here's a gui version of what your looking for.

#include <GuiConstants.au3>

GUICreate("MyGUI", 392, 323)
Dim $phone_num = ""

$Input_1 = GUICtrlCreateInput("", 50, 40, 230, 20, $ES_LEFT)
GUICtrlSetLimit($Input_1, 14)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case Else
            _FormatPhoneNum($Input_1, $phone_num)
    EndSelect
WEnd
Exit

Func _FormatPhoneNum($h_input, ByRef $s_phone_num)
    Local $s_phone = GUICtrlRead($h_input), $char, $t_phone
    If $s_phone_num <> $s_phone Then
        If StringLen($s_phone) > 0 Then
            If StringMid($s_phone, 1, 1) <> "(" Then $s_phone = "(" & $s_phone
            If StringLen($s_phone) >= 2 And StringLen($s_phone) <= 4 Then
                _FixNum($s_phone, 2)
            ElseIf StringLen($s_phone) = 5 Then
                If StringMid($s_phone, 5, 1) <> ")" Then $s_phone = StringMid($s_phone, 1, 4) & ")" & StringMid($s_phone, 5)
            ElseIf StringLen($s_phone) >= 6 And StringLen($s_phone) <= 9 Then
                _FixNum($s_phone, 6)
            ElseIf StringLen($s_phone) >= 10 And StringLen($s_phone) <= 14 Then
                _FixNum($s_phone, 10)
                If StringMid($s_phone, 10, 1) <> "-" Then $s_phone = StringMid($s_phone, 1, 9) & "-" & StringMid($s_phone, 10)
            EndIf
            If StringLen($s_phone) >= 6 Then
                If StringMid($s_phone, 6, 1) <> " " Then $s_phone = StringMid($s_phone, 1, 5) & " " & StringMid($s_phone, 6)
            EndIf
            GUICtrlSetData($h_input, $s_phone)
            $s_phone_num = $s_phone
        EndIf
    EndIf
EndFunc  ;==>_FormatPhoneNum

Func _CheckForNum($s_char); see if value between 0 - 9 inclusive
    If Asc($s_char) >= 48 And Asc($s_char) <= 57 Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc  ;==>_CheckForNum

Func _FixNum(ByRef $s_phone, $Min); if not numbers then remove
    For $x = $Min To StringLen($s_phone)
        $char = StringMid($s_phone, $x, 1)
        If Not _CheckForNum($char) Then
            $t_phone = " " & StringMid($s_phone, $x + 1)
            $s_phone = StringMid($s_phone, 1, $x - 1) & $t_phone
        EndIf
    Next
    $s_phone = StringReplace($s_phone, " ", "")
EndFunc  ;==>_FixNum

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

That's exactly what I was looking for gafrost! Thank you!

Share this post


Link to post
Share on other sites

@gafrost

That's a nice script. Unfortunately, it formats the final number as (123) 4567-890 instead of (123) 456-7890.

I'm sure brainstorm can fix it though. :whistle:


My UDFs: ExitCodes

Share this post


Link to post
Share on other sites

Sorry bout that, sick today

#include <GuiConstants.au3>

GUICreate("MyGUI", 392, 323)
Dim $phone_num = ""

$Input_1 = GUICtrlCreateInput("", 50, 40, 230, 20, $ES_LEFT)
GUICtrlSetLimit($Input_1, 14)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case Else
            _FormatPhoneNum($Input_1, $phone_num)
    EndSelect
WEnd
Exit

Func _FormatPhoneNum($h_input, ByRef $s_phone_num)
    Local $s_phone = GUICtrlRead($h_input), $char, $t_phone
    If $s_phone_num <> $s_phone Then
        If StringLen($s_phone) > 0 Then
            If StringMid($s_phone, 1, 1) <> "(" Then $s_phone = "(" & $s_phone
            If StringLen($s_phone) >= 2 And StringLen($s_phone) <= 4 Then
                _FixNum($s_phone, 2)
            ElseIf StringLen($s_phone) = 5 Then
                If StringMid($s_phone, 5, 1) <> ")" Then $s_phone = StringMid($s_phone, 1, 4) & ")" & StringMid($s_phone, 5)
            ElseIf StringLen($s_phone) >= 6 And StringLen($s_phone) <= 9 Then
                _FixNum($s_phone, 6)
            ElseIf StringLen($s_phone) >= 10 And StringLen($s_phone) <= 14 Then
                _FixNum($s_phone, 10)
                If StringMid($s_phone, 9, 1) <> "-" Then $s_phone = StringMid($s_phone, 1, 8) & "-" & StringMid($s_phone, 9)
            EndIf
            If StringLen($s_phone) >= 6 Then
                If StringMid($s_phone, 6, 1) <> " " Then $s_phone = StringMid($s_phone, 1, 5) & " " & StringMid($s_phone, 6)
            EndIf
            GUICtrlSetData($h_input, $s_phone)
            $s_phone_num = $s_phone
        EndIf
    EndIf
EndFunc  ;==>_FormatPhoneNum

Func _CheckForNum($s_char); see if value between 0 - 9 inclusive
    If Asc($s_char) >= 48 And Asc($s_char) <= 57 Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc  ;==>_CheckForNum

Func _FixNum(ByRef $s_phone, $Min); if not numbers then remove
    For $x = $Min To StringLen($s_phone)
        $char = StringMid($s_phone, $x, 1)
        If Not _CheckForNum($char) Then
            $t_phone = " " & StringMid($s_phone, $x + 1)
            $s_phone = StringMid($s_phone, 1, $x - 1) & $t_phone
        EndIf
    Next
    $s_phone = StringReplace($s_phone, " ", "")
EndFunc  ;==>_FixNum

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

@gafrost

That's a nice script.  Unfortunately, it formats the final number as (123) 4567-890 instead of (123) 456-7890.

I'm sure brainstorm can fix it though. :whistle:

<{POST_SNAPBACK}>

I did fix it. Sorry for not mentioning that.

Share this post


Link to post
Share on other sites

Another related question:

How would I call __FormatPhoneNum if I'm using GUIOnEventMode?

Share this post


Link to post
Share on other sites

#include <GuiConstants.au3>

Opt("GUIOnEventMode",1)

GUICreate("MyGUI", 392, 323)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

Dim $phone_num = ""

$Input_1 = GUICtrlCreateInput("", 50, 40, 230, 20, $ES_LEFT)

GUICtrlSetLimit($Input_1, 14)
GUISetState()

While 1
    Sleep( 250 )
    _FormatPhoneNum($Input_1, $phone_num)
WEnd

Func _FormatPhoneNum($h_input, ByRef $s_phone_num)
    Local $s_phone = GUICtrlRead($h_input), $char, $t_phone
    If $s_phone_num <> $s_phone Then
        If StringLen($s_phone) > 0 Then
            If StringMid($s_phone, 1, 1) <> "(" Then $s_phone = "(" & $s_phone
            If StringLen($s_phone) >= 2 And StringLen($s_phone) <= 4 Then
                _FixNum($s_phone, 2)
            ElseIf StringLen($s_phone) = 5 Then
                If StringMid($s_phone, 5, 1) <> ")" Then $s_phone = StringMid($s_phone, 1, 4) & ")" & StringMid($s_phone, 5)
            ElseIf StringLen($s_phone) >= 6 And StringLen($s_phone) <= 9 Then
                _FixNum($s_phone, 6)
            ElseIf StringLen($s_phone) >= 10 And StringLen($s_phone) <= 14 Then
                _FixNum($s_phone, 10)
                If StringMid($s_phone, 9, 1) <> "-" Then $s_phone = StringMid($s_phone, 1, 8) & "-" & StringMid($s_phone, 9)
            EndIf
            If StringLen($s_phone) >= 6 Then
                If StringMid($s_phone, 6, 1) <> " " Then $s_phone = StringMid($s_phone, 1, 5) & " " & StringMid($s_phone, 6)
            EndIf
            GUICtrlSetData($h_input, $s_phone)
            $s_phone_num = $s_phone
        EndIf
    EndIf
EndFunc  ;==>_FormatPhoneNum

Func _CheckForNum($s_char); see if value between 0 - 9 inclusive
    If Asc($s_char) >= 48 And Asc($s_char) <= 57 Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc  ;==>_CheckForNum

Func _FixNum(ByRef $s_phone, $Min); if not numbers then remove
    For $x = $Min To StringLen($s_phone)
        $char = StringMid($s_phone, $x, 1)
        If Not _CheckForNum($char) Then
            $t_phone = " " & StringMid($s_phone, $x + 1)
            $s_phone = StringMid($s_phone, 1, $x - 1) & $t_phone
        EndIf
    Next
    $s_phone = StringReplace($s_phone, " ", "")
EndFunc  ;==>_FixNum

Func _Exit()
    Exit
EndFunc


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

didn't give you exactly what you were looking for, but gave an example of what needs to be done by creating the Event that calls _Exit()


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

didn't give you exactly what you were looking for, but gave an example of what needs to be done by creating the Event that calls _Exit()

<{POST_SNAPBACK}>

It works just fine. The only draw back is that the response is slow, but I guess there's nothing we can do about that. VBA is spot on no matter how fast you type in the number, where as autoit lags and doesn't format the string properly.

Thanks for all your help tho.

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  
Followers 0