Jump to content

Help with GUI - Enable button when input is populated (Dynamic Update)


Go to solution Solved by pixelsearch,

Recommended Posts

Good afternoon all,

It’s been a while and I’m hoping someone can point me in the right direction…

Background:

I’m creating a utility to enable the administration of user accounts using water's fantastic Active Directory UDF ; I’ve got the basic functionality and now I’m working on the presentation.

The controls which are of particularly interest are an input (Username) and a button (Search)

Goal:

I’d like for the button to be disabled until a user starts typing within the input which will fire off an event which enables the button. However if a user removes the contents from the input; the button is then disabled again.

For the life of me I can't remember how to do that...

Code Snippet:

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

Opt('GUIOnEventMode' , 1)

Global $App = "Test Application"

_BuildGUI()

; Loop until the user exits.
    While 1
        Sleep(250)
    WEnd
_Quit()

Func _BuildGUI()
    Global $hGUI = GUICreate($App, 480, 80, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS), $WS_EX_WINDOWEDGE) ; Create GUI
    GUICtrlCreateLabel("&Username:", 20, 18, 75, 20)    ; Create Username label
    $inp_Username = GUICtrlCreateInput("", 100, 15, 290, 20)    ; Create Username input
    GUICtrlSetOnEvent(-1,"_Check")  ; Call _Check when exit Username input
    Global $btn_Search = GUICtrlCreateButton("&Search", 400, 15, 60, 20)    ; Create Search Button
    GUICtrlSetOnEvent(-1, "_Search")    ; Call _Search function when pressed
    Global $btn_Quit = GUICtrlCreateButton("&Quit", 400, 45, 60, 20)    ; Create Quit Button
    GUICtrlSetOnEvent(-1, "_Quit")  ; Call _Quit function when pressed
    GUICtrlSetState($btn_Search, $GUI_DISABLE)  ; Disable Search Button
    GUISetState(@SW_SHOW, $hGUI)
EndFunc

Func _Check()
    GUICtrlSetState($btn_Search, $GUI_ENABLE)   ; Ena;be Button
EndFunc

Func _Search()
    MsgBox(4096, $App, "Search for user...")    ; DO SOMETHING
EndFunc

Func _Quit()
    GUIDelete($hGUI)    ; Delete the previous GUI and all controls.
    Exit
EndFunc

Thanks in advance

PlayZoneUK

 

Edited by PlayZoneUK

Words of Wisdom and Favourite Quotes:

'Nothing is impossible, despite what other people say and think, prove them wrong and show them what you can do!'

'Understanding is a three edged sword, your side, their side and the truth!'

'The truth that humanity has never been able to grasp, is that death may be the only absolute freedom there is!'

'I've run out of places to put the brush... what do you suggest?'

Link to comment
Share on other sites

Maybe something like:

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

Opt('GUIOnEventMode' , 1)

Global $hGUI, $inp_Username, $btn_Search, $btn_Quit
Global $App = "Test Application"

_BuildGUI()

While 1
    Sleep(250)
WEnd
_Quit()

Func _BuildGUI()
    $hGUI = GUICreate($App, 480, 80, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS), $WS_EX_WINDOWEDGE) ; Create GUI
    GUICtrlCreateLabel("&Username:", 20, 18, 75, 20)    ; Create Username label

    $inp_Username = GUICtrlCreateInput("", 100, 15, 290, 20)    ; Create Username input
        GUICtrlSetOnEvent(-1,"_Check")  ; Call _Check when exit Username input

    $btn_Search = GUICtrlCreateButton("&Search", 400, 15, 60, 20)    ; Create Search Button
        GUICtrlSetOnEvent(-1, "_Search")    ; Call _Search function when pressed

    $btn_Quit = GUICtrlCreateButton("&Quit", 400, 45, 60, 20)    ; Create Quit Button
        GUICtrlSetOnEvent(-1, "_Quit")  ; Call _Quit function when pressed

    GUISetState(@SW_SHOW, $hGUI)

    AdlibRegister("_EnableSearch")

EndFunc

Func _Check()
    GUICtrlSetState($btn_Search, $GUI_ENABLE)   ; Ena;be Button
EndFunc

Func _Search()
    MsgBox(4096, $App, "Search for user...")    ; DO SOMETHING
EndFunc

Func _Quit()
    GUIDelete($hGUI)    ; Delete the previous GUI and all controls.
    Exit
EndFunc

Func _EnableSearch()
    Local $sUsername = GUICtrlRead($inp_Username)
    Select
        Case $sUsername = ""
            GUICtrlSetState($btn_Search, $GUI_DISABLE)
        Case Else
            GUICtrlSetState($btn_Search, $GUI_ENABLE)
    EndSelect
EndFunc

 

Link to comment
Share on other sites

  • Solution

Hi Subz :)
Your solution works fine.

Though in these cases, I prefer to register WM_COMMAND and test $EN_UPDATE (or $EN_CHANGE) etc...
It allows to enable/disable the button at the exact moment when the input field is filled/emptied, without that little "visual delay" due to AdlibRegister (max 250ms by default, though it maybe diminished but it will then check AdlibRegister more frequently)

Also, WM_COMMAND is only triggered when the user interferes with the controls (which is less frequent than repeated checks to AdlibRegister)

This is a version with a registered WM_COMMAND :

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

Opt('GUIOnEventMode' , 1)

Global $hGUI, $inp_Username, $btn_Search, $btn_Quit
Global $App = "Test Application"

_BuildGUI()

While 1
    Sleep(250)
WEnd
_Quit()

Func _BuildGUI()
    $hGUI = GUICreate($App, 480, 80, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS), $WS_EX_WINDOWEDGE) ; Create GUI
    GUICtrlCreateLabel("&Username:", 20, 18, 75, 20)    ; Create Username label

    $inp_Username = GUICtrlCreateInput("", 100, 15, 290, 20)    ; Create Username input

    $btn_Search = GUICtrlCreateButton("&Search", 400, 15, 60, 20)    ; Create Search Button
        GUICtrlSetOnEvent(-1, "_Search")    ; Call _Search function when pressed
        GUICtrlSetState(-1, $GUI_DISABLE)

    $btn_Quit = GUICtrlCreateButton("&Quit", 400, 45, 60, 20)    ; Create Quit Button
        GUICtrlSetOnEvent(-1, "_Quit")  ; Call _Quit function when pressed

    GUISetState(@SW_SHOW, $hGUI)
    GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')
EndFunc

Func _Search()
    MsgBox(4096, $App, "Search for user...")    ; DO SOMETHING
EndFunc

Func _Quit()
    GUIDelete($hGUI)    ; Delete the previous GUI and all controls.
    Exit
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Local $nNotifyCode = BitShift($wParam, 16)

    Switch $lParam
        Case GUICtrlGetHandle($inp_Username)
            Switch $nNotifyCode
                Case $EN_UPDATE ; "just before text is displayed."
                    ; $EN_CHANGE would work too ("after the system updates the screen.")
                    ; Local Static $iEn_Update = 0
                    ; $iEn_Update +=1
                    ; ConsoleWrite($iEn_Update & " $iEn_Update >" & GUICtrlRead($inp_Username) & "<" & @crlf)
                    GUICtrlSetState($btn_Search, (GUICtrlRead($inp_Username) = "" ? $GUI_DISABLE : $GUI_ENABLE))
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

Have a great sunday.

Link to comment
Share on other sites

@Subz & @pixelsearch thank you for your suggestions, before I went to sleep I had come across references to the WM_COMMAND and $EN_CHANGE. I'd attempted to implement it but I was only missing the following statement:

GUICtrlSetState($btn_Search, (GUICtrlRead($inp_Username) = "" ? $GUI_DISABLE : $GUI_ENABLE))

I've incorporated the above line and utilised $EN_UPDATE into my code and it's working perfectly...

Once again thank you both for your assistance it is much appreciated.

 

PlayZoneUK

Words of Wisdom and Favourite Quotes:

'Nothing is impossible, despite what other people say and think, prove them wrong and show them what you can do!'

'Understanding is a three edged sword, your side, their side and the truth!'

'The truth that humanity has never been able to grasp, is that death may be the only absolute freedom there is!'

'I've run out of places to put the brush... what do you suggest?'

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