Jump to content

I want to check if my input box contains only numbers when its changed


Misha
 Share

Recommended Posts

I want to check if my input box contains only numbers when its changed, how will I be doing that?

I tried guictrlsetonevent but the function in there cant get variables so I cant give the function the control name.

Oh and I do not want to make a function for every input..making my bot 300 more lines is not cool ;_;.

Thank you.

Edited by Misha
Link to comment
Share on other sites

check this example it may help you

#include <GUIConstants.au3>

GUICreate("test", 200, 100)

$put_1 = GUICtrlCreateInput("", 10, 10, 180, 25)
$do = GUICtrlCreateButton("test", 10, 50, 90, 25)
GUICtrlSetFont(-1, 12, 600)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $do Then
        If GUICtrlRead ($put_1) = "" Then
            MsgBox (-1, "", "your input is Empty")
        EndIf
        If Number (GUICtrlRead ($put_1)) = 0 Then
        MsgBox (-1, " info ", " no nuber only letters or empty input")
        EndIf
        If Number (GUICtrlRead ($put_1)) <> 0 Then
        MsgBox (-1, " info ", " you have entered this value of Numbers" & @CRLF & @CRLF & Number (GUICtrlRead ($put_1)))
        EndIf
EndIf
WEnd
Edited by star2

[quote]Baby you're all that I want, When you're lyin' here in my armsI'm findin' it hard to believe, We're in heavenAnd love is all that I need , And I found it there in your heartIt isn't too hard to see, We're in heaven .Bryan Adams[/quote].............................................................................[u]AUTOIT[/u]

Link to comment
Share on other sites

That wouldnt be automatic...I just want the user to be able to input only numebers in the input boxes..Thanks anyway;

Ok check this out

#include <GUIConstants.au3>

GUICreate("test", 200, 100)

$put_1 = GUICtrlCreateInput("", 10, 10, 180, 25)
$do = GUICtrlCreateButton("test", 10, 50, 90, 25)
GUICtrlSetFont(-1, 12, 600)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If GUICtrlRead ($put_1) And Number (GUICtrlRead ($put_1)) = 0 Then
        MsgBox (-1, "", "YOU ARE NOT ALLOWED TO USE LETTERS")
        GUICtrlSetData ($put_1, "")
    EndIf
    If $msg = $do Then
        If GUICtrlRead ($put_1) = "" Then
            MsgBox (-1, "", "your input is Empty")
        EndIf
        If Number (GUICtrlRead ($put_1)) <> 0 Then
        MsgBox (-1, " info ", " you have entered this value of Numbers" & @CRLF & @CRLF & Number (GUICtrlRead ($put_1)))
        EndIf
EndIf
WEnd
Edited by star2

[quote]Baby you're all that I want, When you're lyin' here in my armsI'm findin' it hard to believe, We're in heavenAnd love is all that I need , And I found it there in your heartIt isn't too hard to see, We're in heaven .Bryan Adams[/quote].............................................................................[u]AUTOIT[/u]

Link to comment
Share on other sites

ahaha that is an awesome way, too bad I use onevent.. still awesome way,good idea.

Glad u liked it.

see ya !!

[quote]Baby you're all that I want, When you're lyin' here in my armsI'm findin' it hard to believe, We're in heavenAnd love is all that I need , And I found it there in your heartIt isn't too hard to see, We're in heaven .Bryan Adams[/quote].............................................................................[u]AUTOIT[/u]

Link to comment
Share on other sites

ahaha that is an awesome way, too bad I use onevent.. still awesome way,good idea.

On event doesn't effect the method at all.

Here is a small mod to star2's post

#include <GUIConstants.au3>

GUICreate("test", 200, 100)
Opt("GUIOnEventMode",1)
$put_1 = GUICtrlCreateInput("", 10, 10, 180, 25)
$do = GUICtrlCreateButton("test", 10, 50, 90, 25)
GUICtrlSetFont(-1, 12, 600)
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "EXITPROG")
$lasted = ''
While 1
   $newed = GUICtrlRead ($put_1)
    If  $newed <> $lasted Then
        $lasted = ''    
        For $nc = 0 To StringLen($newed)
            $cc = StringMid($newed,$nc,1)
            If Asc($cc) >= Asc('0') And Asc($cc) <= Asc('9') Then
                $lasted = $lasted & $cc
            EndIf
        Next
     GUICtrlSetData ($put_1, $lasted)
     EndIf

WEnd

Func ExitPROG()
Exit;   
EndFunc

@star2 - Your method didn't quite work because it allowed digits to be followed by letters since Number still returns a value for '123kkr' say.

Edit: correct error in script

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

On event doesn't effect the method at all.

Here is a small mod to star2's post

@star2 - Your method didn't quite work because it allowed digits to be followed by letters since Number still returns a value for '123kkr' say.

Edit: correct error in script

Nice job I wasn't carefull in my post

thanks for the tip

seee ya

[quote]Baby you're all that I want, When you're lyin' here in my armsI'm findin' it hard to believe, We're in heavenAnd love is all that I need , And I found it there in your heartIt isn't too hard to see, We're in heaven .Bryan Adams[/quote].............................................................................[u]AUTOIT[/u]

Link to comment
Share on other sites

Eeehm...use $ES_NUMBER style on input.

u really Killed me with your reply

I really love it

I don't know what to say

the best way to learn is to have the best as a teatcher

thanks

[quote]Baby you're all that I want, When you're lyin' here in my armsI'm findin' it hard to believe, We're in heavenAnd love is all that I need , And I found it there in your heartIt isn't too hard to see, We're in heaven .Bryan Adams[/quote].............................................................................[u]AUTOIT[/u]

Link to comment
Share on other sites

According to Helge

#include <GUIConstants.au3>

GUICreate("test", 200, 100)

$put_1 = GUICtrlCreateInput("", 10, 10, 180, 25,$ES_NUMBER)
$do = GUICtrlCreateButton("test", 10, 50, 90, 25)
GUICtrlSetFont(-1, 12, 600)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $do Then
        If GUICtrlRead ($put_1) = "" Then
            MsgBox (-1, "", "your input is Empty")
        EndIf
        If Number (GUICtrlRead ($put_1)) <> 0 Then
        MsgBox (-1, " info ", " you have entered this value of Numbers" & @CRLF & @CRLF & Number (GUICtrlRead ($put_1)))
        EndIf
    EndIf
WEnd

[quote]Baby you're all that I want, When you're lyin' here in my armsI'm findin' it hard to believe, We're in heavenAnd love is all that I need , And I found it there in your heartIt isn't too hard to see, We're in heaven .Bryan Adams[/quote].............................................................................[u]AUTOIT[/u]

Link to comment
Share on other sites

Eeehm...use $ES_NUMBER style on input.

Yes, that did make us look a bit silly.

But in our defense, star's method with a small mod does allow for real numbers to be entered.

(Hope you're not going to tell me there's $ES_REAL)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Another Method:

#include <GuiConstants.au3>

Opt("GUIOnEventMode", 1)

Global Const $WM_COMMAND = 0x0111
Global Const $EN_CHANGE = 0x300
Global Const $DebugIt = 1

$GUI = GUICreate(" My GUI", 500, 500)
$input1 = GUICtrlCreateInput("", 10, 5, 300, 20)
$input2 = GUICtrlCreateInput("", 10, 35, 300, 20)
$input3 = GUICtrlCreateInput("", 10, 65, 300, 20)
$input4 = GUICtrlCreateInput("", 10, 95, 300, 20)
GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "EXITPROG")
GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")

$msg = 0
While 1
    Sleep(10)
WEnd

Func ExitPROG()
    Exit
EndFunc   ;==>ExitPROG

Func _Input_Changed(ByRef $input_control)
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then _DebugPrint("Input Changed:" & GUICtrlRead($input_control))
    ;----------------------------------------------------------------------------------------------
    Local $tmp_string = GUICtrlRead($input_control)
    Local $tmp_char = StringMid($tmp_string, StringLen($tmp_string))
    If (Asc($tmp_char) >= Asc('0') And Asc($tmp_char) <= Asc('9')) Or Asc($tmp_char) = Asc('.') Then
        ;----------------------------------------------------------------------------------------------
        If $DebugIt Then _DebugPrint("Valid Input:" & $tmp_char)
        ;----------------------------------------------------------------------------------------------
    Else
        ;----------------------------------------------------------------------------------------------
        If $DebugIt Then _DebugPrint("Invalid Input:" & $tmp_char)
        ;----------------------------------------------------------------------------------------------
        GUICtrlSetData($input_control, StringMid($tmp_string, 1, StringLen($tmp_string) - 1))
    EndIf
    
EndFunc   ;==>_Input_Changed

Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local $nNotifyCode = _HiWord($wParam)
    Local $nID = _LoWord($wParam)
    Local $hCtrl = $lParam

    Switch $nID
        Case $input1, $input2, $input3, $input4
            Switch $nNotifyCode
                Case $EN_CHANGE
                    _Input_Changed($nID)
            EndSwitch
    EndSwitch
    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_COMMAND

Func _DebugPrint($s_text)
    $s_text = StringReplace($s_text, @LF, @LF & "-->")
    ConsoleWrite("!===========================================================" & @LF & _
            "+===========================================================" & @LF & _
            "-->" & $s_text & @LF & _
            "+===========================================================" & @LF)
EndFunc   ;==>_DebugPrint


Func _HiWord($x)
    Return BitShift($x, 16)
EndFunc   ;==>_HiWord

Func _LoWord($x)
    Return BitAND($x, 0xFFFF)
EndFunc   ;==>_LoWord

SciTE for AutoItDirections for Submitting Standard UDFs

 

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

 

Link to comment
Share on other sites

Yea, gafrost I was planning to making that myself, well $ES_NUMBER seems much more simpler.

True, but if you want to use int or real numbers then $ES_NUMBER won't do the job for the reals

SciTE for AutoItDirections for Submitting Standard UDFs

 

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

 

Link to comment
Share on other sites

True, but if you want to use int or real numbers then $ES_NUMBER won't do the job for the reals

@gafrost,

Your EN_CHANGE is the best way, but your _input_changed function is faulty. It allows people to insert letters between digits, which is one of the reasons I modified star's method. You have to check the whole of the edit not just the end.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

@gafrost,

Your EN_CHANGE is the best way, but your _input_changed function is faulty. It allows people to insert letters between digits, which is one of the reasons I modified star's method. You have to check the whole of the edit not just the end.

It was a quick and dirty, shouldn't be that hard to change to what you want.

Replace the function with this one from what I posted above.

Func _Input_Changed(ByRef $input_control)
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then _DebugPrint("Input Changed:" & GUICtrlRead($input_control))
    ;----------------------------------------------------------------------------------------------
    Local $tmp_string = GUICtrlRead($input_control), $tmp_char, $tmp_input = ""
    For $x = 1 To StringLen($tmp_string)
        $tmp_char = StringMid($tmp_string, $x, 1)
        If (Asc($tmp_char) >= Asc('0') And Asc($tmp_char) <= Asc('9')) Or Asc($tmp_char) = Asc(".") Then $tmp_input &= $tmp_char
    Next
    GUICtrlSetData($input_control, $tmp_input)
EndFunc   ;==>_Input_Changed
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

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

 

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...