Jump to content

Detect Input Box changes


OzoneB
 Share

Recommended Posts

I have an AutoIT GUI with an input box. I want to run a check on the input every time the value changes. For example:

$myvalue = "hello"
$value = GUICtrlCreateInput("",150,75,190,20)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case ????????
            If GuiCtrlRead($value) == $myvalue Then
                ; Enable next field.
            Else
                ; Disable next field.
            EndIf
     EndSwitch
WEnd

So any time a key is pressed in that input box the if statement gets executed. I don't know what I should put where I've put the ???? at.

Thank you!

Link to comment
Share on other sites

Hi @OzoneB, and welcome to the AutoIt forums :welcome:
You have to "capture" the event "change" sent from the Input box :)
To do this, you use a WM_COMMAND Windows Message, capturing the EN_CHANGE Event/Notification code.
A little sample for the welcome :)

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

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=
Global $frmMainForm = GUICreate("A Form", 235, 62, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApplication")
Global $txtInput = GUICtrlCreateInput("", 19, 22, 201, 21)
GUICtrlSetFont(-1, 10, 400)
GUISetState(@SW_SHOW, $frmMainForm)
#EndRegion ### END Koda GUI section ###

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
    Sleep(100)
WEnd


Func ExitApplication()
    Exit
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    Local $hdlWindowFrom, _
          $intMessageCode, _
          $intControlID_From

    $intControlID_From =  BitAND($wParam, 0xFFFF)
    $intMessageCode = BitShift($wParam, 16)

    Switch $intControlID_From
        Case $txtInput
            Switch $intMessageCode
                Case $EN_CHANGE
                    ConsoleWrite("[" & _Now() & "] - The text in the $txtInput control has changed! Text = " & GUICtrlRead($txtInput) & @CRLF)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc

 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • 1 year later...
On 12/13/2018 at 11:01 AM, FrancescoDiMuro said:

Hi @OzoneB, and welcome to the AutoIt forums :welcome:
You have to "capture" the event "change" sent from the Input box :)
To do this, you use a WM_COMMAND Windows Message, capturing the EN_CHANGE Event/Notification code.
A little sample for the welcome :)

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

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=
Global $frmMainForm = GUICreate("A Form", 235, 62, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApplication")
Global $txtInput = GUICtrlCreateInput("", 19, 22, 201, 21)
GUICtrlSetFont(-1, 10, 400)
GUISetState(@SW_SHOW, $frmMainForm)
#EndRegion ### END Koda GUI section ###

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
    Sleep(100)
WEnd


Func ExitApplication()
    Exit
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    Local $hdlWindowFrom, _
          $intMessageCode, _
          $intControlID_From

    $intControlID_From =  BitAND($wParam, 0xFFFF)
    $intMessageCode = BitShift($wParam, 16)

    Switch $intControlID_From
        Case $txtInput
            Switch $intMessageCode
                Case $EN_CHANGE
                    ConsoleWrite("[" & _Now() & "] - The text in the $txtInput control has changed! Text = " & GUICtrlRead($txtInput) & @CRLF)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG

EndFunc

 

Hello, I need something like this to check changes on a input box...But I tried your example and some others from the forum and nothing worked...Your post is the more recent, I hope it don't count as a necropost...I looked into the rules and couldn't find any expiration date...

Now Im trying checks inside a loop but the creatinput keep flashing and I'm having trouble trying to define a pause...

Thanks!

Link to comment
Share on other sites

19 minutes ago, Nine said:

Post your own code, so we can see what is wrong...

I tried a lot of If's And's switch and everything...I'm thinking to change the approach to this WM_COMMAND...Maybe will be a cleaner solution...The way I'm checking the input keeps flashing and turns even difficult to type!

the code right now...

;text from input
    Global $destino = ControlGetText("GUI2", "", $Final)
    ;check if have at least 3 octets
    Global $StartSpliting = StringRegExp($destino,"((?:\d{1,3}\.){2}\d{1,3})")
    ;check if all octets have been filled
    Global $IPFull = StringRegExp($destino,"((?:\d{1,3}\.){3}\d{1,3})")
    ;MsgBox(48, "", $StartSpliting,20)
    
;empty branch number
If $destino = "" And $Filial <> $destino then
        $Filial = $destino
        GUICtrlSetData($labelfilialdest,"Filial: ")
EndIf


If $StartSpliting = 1 Then
Global $IPdestino = StringSplit($destino,".")

            If $IPdestino[1] = 10 And $IPdestino[2] < 254 Then
                    If $IPdestino[2] <> $Filial Then
                            $Filial = $IPdestino[2]
                            GUICtrlSetData($labelfilialdest,"Filial: " & $Filial)
                            ;MsgBox(48, "", $destino & "segundo octeto")
                            GUICtrlSetColor($Final, 0xFF0000)
                    EndIf

            ElseIf $IPdestino[1] = 10 And $IPdestino[2] = 254 And $IPdestino[3] = 0 Then
                    If $IPdestino[2] <> $Filial Then
                            $Filial = $IPdestino[2]
                            GUICtrlSetData($labelfilialdest,"Filial: " & $Filial)
                            ;MsgBox(48, "", $destino & "segundo octeto")
                            GUICtrlSetColor($Final, 0xFF0000)
                    EndIf

            ElseIf $IPdestino[1] = 10 And $IPdestino[2] == 254 And $IPdestino[3] >= 1 Then
                    If $IPdestino[2] + $IPdestino[3] <> $Filial Then
                            $Filial = $IPdestino[2]+$IPdestino[3]
                            GUICtrlSetData($labelfilialdest,"Filial: " & $Filial)
                            ;MsgBox(48, "", $destino & "segundo e terceiro octeto")
                            GUICtrlSetColor($Final, 0xFF0000)
                    EndIf
            EndIf

ElseIf $IPFull = 1 Then
Global $IPdestino = StringSplit($destino,".")
            Global $Return = _ValidIP($destino)
                    If $Return < 0 Then
                            Switch @error
                                    Case 1
                                            MsgBox(64, "", "Error in IP address: " & $destino & @LF & "Error code is: 1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239")
                                    Case 2
                                            MsgBox(64, "", "Error in IP address: " & $destino & @LF & "Error code is: 2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters")
                                    Case 3
                                            MsgBox(64, "", "Error in IP address: " & $destino & @LF & "Error code is: 3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20)")
                                    Case 4
                                            MsgBox(64, "", "Error in IP address: " & $destino & @LF & "Error code is: 4 - Last octet is either 0 or 255")
                            EndSwitch
                    Else
                            GUICtrlSetColor($Final, 0x00CC00)
                            ;MsgBox(48, "", $destino & " is a valid Class " & $Return[5] & " IP address")
                    EndIf

 

Edited by edumanilha
worng code
Link to comment
Share on other sites

Link to comment
Share on other sites

48 minutes ago, Nine said:

Sorry but this code is not runable.  Please make it short and runable.  Remove all unnecessary code and leave what is causing the problem...

#NoTrayIcon

#include <GUIConstants.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <GDIPlus.au3>
#include <GuiButton.au3>
#include <GuiImageList.au3>
#include <WinAPIGdi.au3>
#include <SendMessage.au3>

$IP1 = StringSplit("192.168.6.10",".")

;CHECAR REDE
$Octeto = $IP1
$Filial = $Octeto[2] + $Octeto [3]



$Form2 = GUICreate ("GUI2", 600, 190)
GUICtrlCreatePic(@ScriptDir & "\Dados\Imagens\background.bmp",0,0,0,0,$SS_WHITERECT)
GUISetState(@SW_SHOW, $Form2)


;botão 4 insert IP
$Final = GUICtrlCreateInput($Octeto[1] & "." & $Octeto[2] & "." & $Octeto[3] & ".", 435, 40, 140, 20,$SS_CENTER)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetFont(-1,11,600,0,"Arial",5)
GUICtrlSetTip(-1, "Preenche ao abrir, com seus 3 primeiros octetos da sua rede atual." & @CRLF &  "Caso deseje conectar em outro local, basta apagar e preencher o IP que deseja.")
WinSetOnTop(-1, "", $WINDOWS_ONTOP)
GUICtrlSetBkColor(-1, 0x000000)
;GuiCtrlSetState($Form2, $GUI_ONTOP)
;Global $destino = ControlGetText("GUI2", "", $Final)

                ;comentário botão user/comandos
                Global $labelfilialdest = GUICtrlCreateLabel("Filial: " & $Filial, 430, 65, 150, 20,$SS_CENTER)
                GUICtrlSetColor(-1, 0x00000)
                GUICtrlSetFont(-1, 11, 600, 0, "Arial", 5)

while 1

;text from input
    Global $destino = ControlGetText("GUI2", "", $Final)
    ;check if have at least 3 octets
    Global $StartSpliting = StringRegExp($destino,"((?:\d{1,3}\.){2}\d{1,3})")
    ;check if all octets have been filled
    Global $IPFull = StringRegExp($destino,"((?:\d{1,3}\.){3}\d{1,3})")
    ;MsgBox(48, "", $StartSpliting,20)

;empty branch number
If $destino = "" And $Filial <> $destino then
        $Filial = $destino
        GUICtrlSetData($labelfilialdest,"Filial: ")
EndIf


If $StartSpliting = 1 Then
Global $IPdestino = StringSplit($destino,".")

            If $IPdestino[1] = 10 And $IPdestino[2] < 254 Then
                    If $IPdestino[2] <> $Filial Then
                            $Filial = $IPdestino[2]
                            GUICtrlSetData($labelfilialdest,"Filial: " & $Filial)
                            ;MsgBox(48, "", $destino & "segundo octeto")
                            GUICtrlSetColor($Final, 0xFF0000)
                    EndIf

            ElseIf $IPdestino[1] = 10 And $IPdestino[2] = 254 And $IPdestino[3] = 0 Then
                    If $IPdestino[2] <> $Filial Then
                            $Filial = $IPdestino[2]
                            GUICtrlSetData($labelfilialdest,"Filial: " & $Filial)
                            ;MsgBox(48, "", $destino & "segundo octeto")
                            GUICtrlSetColor($Final, 0xFF0000)
                    EndIf

            ElseIf $IPdestino[1] = 10 And $IPdestino[2] == 254 And $IPdestino[3] >= 1 Then
                    If $IPdestino[2] + $IPdestino[3] <> $Filial Then
                            $Filial = $IPdestino[2]+$IPdestino[3]
                            GUICtrlSetData($labelfilialdest,"Filial: " & $Filial)
                            ;MsgBox(48, "", $destino & "segundo e terceiro octeto")
                            GUICtrlSetColor($Final, 0xFF0000)
                    EndIf
            EndIf
EndIf


If $IPFull = 1 Then
Global $IPdestino = StringSplit($destino,".")
            Global $Return = _ValidIP($destino)
                    If $Return < 0 Then
                            Switch @error
                                    Case 1
                                            MsgBox(64, "", "Error in IP address: " & $destino & @LF & "Error code is: 1 - IP address starts with an invalid number = 0, 127 , 169 or is > 239")
                                    Case 2
                                            MsgBox(64, "", "Error in IP address: " & $destino & @LF & "Error code is: 2 - one of the octets of the IP address is out of the range 0-255 or contains invalid characters")
                                    Case 3
                                            MsgBox(64, "", "Error in IP address: " & $destino & @LF & "Error code is: 3 - IP Address is not a valid dotted IP address (ex. valid address 190.40.100.20)")
                                    Case 4
                                            MsgBox(64, "", "Error in IP address: " & $destino & @LF & "Error code is: 4 - Last octet is either 0 or 255")
                            EndSwitch
                    Else
                            GUICtrlSetColor($Final, 0x00CC00)
                            ;MsgBox(48, "", $destino & " is a valid Class " & $Return[5] & " IP address")
                    EndIf
Else
                            GUICtrlSetColor($Final, 0xFF0000)
EndIf
WEnd

Func _ValidIP($sIP)
    Local $adIPAddressInfo[6]

    Local $aArray = StringSplit($sIP, ".", 2)

    If Not IsArray($aArray) Or UBound($aArray) <> 4 Then Return SetError(3, 0, -1)

    Local $dString = "0x"

    If $aArray[0] <= 0 Or $aArray[0] > 239 Or $aArray[0] = 127 Or $aArray[0] = 169 Then
        Return SetError(1, 0, -1)
    EndIf

    For $I = 0 To 3
        If $I < 3 Then
            If $aArray[$I] < 0 Or $aArray[$I] > 255 Or Not StringIsDigit($aArray[$I]) Then
                Return SetError(2, 0, -1)
            EndIf
        Else
            If Not StringIsDigit($aArray[$I]) Then
                Return SetError(2, 0, -1)
            EndIf

            If $aArray[$I] < 1 Or $aArray[$I] > 254 Then
                Return SetError(4, 0, -1)
            EndIf
        EndIf

        $dString &= StringRight(Hex($aArray[$I]), 2)

        $adIPAddressInfo[$I] = $aArray[$I]
    Next

    $adIPAddressInfo[4] = $dString

    Switch $aArray[0]
        Case 1 To 126
            $adIPAddressInfo[5] = "A"
            Return $adIPAddressInfo
        Case 128 To 191
            $adIPAddressInfo[5] = "B"
            Return $adIPAddressInfo
        Case 192 To 223
            $adIPAddressInfo[5] = "C"
            Return $adIPAddressInfo
        Case 224 To 239
            $adIPAddressInfo[5] = "D"
            Return $adIPAddressInfo
    EndSwitch
EndFunc   ;==>_ValidIP

I kept only the basic now...Thanks! But About WM_COMMAND, do you have any working example? It may be usefull tomorrow...

EDIT: I found one WM working example!

Edited by edumanilha
Link to comment
Share on other sites

@edumanilha Just use _GuiCtrlIPAddress_... functions example:

#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>

Example()

Func Example()
    Local $sIPAddress = "192.168.6.10"
    Local $hGui = GUICreate("IP Address Control", 260, 30)
    Local $hIPAddress = _GUICtrlIpAddress_Create($hGui, 5, 5, 125, 20)
    Local $idIPAddress = GUICtrlCreateButton("IP Address", 130, 4, 125, 22)
    GUISetState(@SW_SHOW)
    _GUICtrlIpAddress_Set($hIPAddress, $sIPAddress)
    While 1
        $nMsg = GUIGetMsg()
            Switch $nMsg
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $idIPAddress
                    MsgBox(4096, "IP Address", "IP Address = " & _GUICtrlIpAddress_Get($hIPAddress))
            EndSwitch
    WEnd
EndFunc

 

Edited by Subz
Link to comment
Share on other sites

9 minutes ago, Subz said:

@edumanilha Just use _GuiCtrlIPAddress_... functions example:

#include <GUIConstantsEx.au3>
#include <GuiIPAddress.au3>

Example()

Func Example()
    Local $sIPAddress = "192.168.6.10"
    Local $hGui = GUICreate("IP Address Control IsBlank Example", 260, 30)
    Local $hIPAddress = _GUICtrlIpAddress_Create($hGui, 5, 5, 125, 20)
    Local $idIPAddress = GUICtrlCreateButton("IP Address", 130, 4, 125, 22)
    GUISetState(@SW_SHOW)
    _GUICtrlIpAddress_Set($hIPAddress, $sIPAddress)
    While 1
        $nMsg = GUIGetMsg()
            Switch $nMsg
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $idIPAddress
                    MsgBox(4096, "IP Address", "IP Address = " & _GUICtrlIpAddress_Get($hIPAddress))
            EndSwitch
    WEnd
EndFunc

 

OMG! Thank you...I didn't saw this...

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