Jump to content

How to disable GUI button if no data or input box is empty?


Recommended Posts

I wrote the following simple script to capture asset information.  I would like the START button to be remain disabled when the default data "ServiceTag" is unchanged or if there is no data or zero data input.  I am having trouble disabling the GUI button after the data has changed from "ServiceTag" to no data or input is empty.  Your input is highly appreciated and thank you.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 168, 148, 192, 124)
Global $Input1 = GUICtrlCreateInput("ServiceTag", 24, 40, 121, 21)
Global $Label1 = GUICtrlCreateLabel("Serial#", 24, 24, 37, 17)
Global $Button1 = GUICtrlCreateButton("Start", 72, 112, 75, 25)
Global $Input2 = GUICtrlCreateInput("Input2", 24, 88, 121, 21)
Global $Label2 = GUICtrlCreateLabel("Asset#", 24, 72, 37, 17)
GUICtrlSetState($Button1, $GUI_DISABLE)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            $sNI_Detected = GUICtrlRead($Input1)
            If $sNI_Detected <> "ServiceTag" Then
                GUICtrlSetState($Button1, $GUI_ENABLE)
            ElseIf GUICtrlRead($Input1) = 0 Then
                GUICtrlSetState($Button1, $GUI_DISABLE)
            EndIf
    EndSwitch
WEnd

 

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 168, 148, 192, 124)
Global $Input1 = GUICtrlCreateInput("ServiceTag", 24, 40, 121, 21)
Global $Label1 = GUICtrlCreateLabel("Serial#", 24, 24, 37, 17)
Global $Button1 = GUICtrlCreateButton("Start", 72, 112, 75, 25)
Global $Input2 = GUICtrlCreateInput("Input2", 24, 88, 121, 21)
Global $Label2 = GUICtrlCreateLabel("Asset#", 24, 72, 37, 17)
GUICtrlSetState($Button1, $GUI_DISABLE)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

GUIRegisterMsg($WM_COMMAND, WM_COMMAND)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    Local $iIDFrom = BitAND($wParam, 0xFFFF) ; Low Word
    Local $iCode = BitShift($wParam, 16) ; Hi Word

    Switch ($hWnd)
        Case $Form1
            Switch ($iIDFrom)
                Case $Input1
                    Switch ($iCode)
                        Case $EN_CHANGE
                            If (GUICtrlRead($iIDFrom) <> "ServiceTag") Then
                                GUICtrlSetState($Button1, $GUI_ENABLE)
                            Else
                                GUICtrlSetState($Button1, $GUI_DISABLE)
                            EndIf
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

 

Link to comment
Share on other sites

I couldn't get the above code to work (mostly due to my lack of understanding it).  However, I think I found a working solution after thinking this through.  Please let me know if the  follow code can be written better.  I am new and am still learning so any and all advice is appreciated.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 168, 148, 192, 124)
Global $Input1 = GUICtrlCreateInput("", 24, 40, 121, 21)
Global $Label1 = GUICtrlCreateLabel("Serial#", 24, 24, 37, 17)
Global $Button1 = GUICtrlCreateButton("Start", 72, 112, 75, 25)
Global $Input2 = GUICtrlCreateInput("Input2", 24, 88, 121, 21)
Global $Label2 = GUICtrlCreateLabel("Asset#", 24, 72, 37, 17)
GUICtrlSetState($Button1, $GUI_DISABLE)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $No_Data
$No_Data = ""

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input1
            $sNI_Detected = GUICtrlRead($Input1)
            If $sNI_Detected = $No_Data Then
                GUICtrlSetState($Button1, $GUI_DISABLE)
            Else
                GUICtrlSetState($Button1, $GUI_ENABLE)
            EndIf
    EndSwitch
WEnd

 

Link to comment
Share on other sites

  • 3 weeks later...

@InunoTaishou -  Thank you for your example code.  I was unable to understand it and could not make it work in my previous attempts.  I am glad I went back to carefully study/research your example this past weekend.  I finally understand how it work and have used it successfully in my scripts.  I just want to say thank you!

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