Sign in to follow this  
Followers 0
Mecano

Disable button if input is empty and checkbox unchecked

8 posts in this topic

Hallo forum members,

Question?

checkbox1 checked, input filled in, then  Button1 active.

I think this question is ask many times but the search results brings me further away.

First start script works good, "Button1" is disabled, when input box is filled in "Button1" is still disabled, when $Checkbox1 is checked "Button1" is enabled so far so good.
But when I uncheck $Checkbox1 "Button1" is still active!
 

The script, forgot to tell that the script have to start up with $Checkbox1 checked.

#NoTrayIcon
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ###
$Form1 = GUICreate("Form1", 401, 193, 192, 124)
$Input1 = GUICtrlCreateInput("", 120, 16, 160, 21)
$Button1 = GUICtrlCreateButton("Button1", 120, 72, 160, 25)
GUICtrlSetState($Button1, $GUI_DISABLE)
$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 122, 128, 97, 17)
GUICtrlSetState($Checkbox1, $GUI_CheckED)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###


$i = 1
While 1
    If BitAnd(GUICtrlRead($Checkbox1),$GUI_UNCHECKED) = $GUI_UNCHECKED And GUICtrlRead($Input1) = ""  And $i = 1 Then
        GUICtrlSetState($Button1, $GUI_DISABLE)
        $i = 0
        MsgBox(0, "Unchecked", "Test unchecked", 10)
    Else
        If $i = 0 And BitAnd(GUICtrlRead($Checkbox1),$GUI_CHECKED) = $GUI_CHECKED And GUICtrlRead($Input1) <> "" Then
            GUICtrlSetState($Button1, $GUI_ENABLE)
            $i = 1
            MsgBox(0, "Checked", "Test checked", 10)
        EndIf
    EndIf

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            ; do your thing
    EndSwitch
WEnd

 

Part of the source

Thanks in advance.

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

works, now...this disables if not checked OR StringLen = 0...so enabled only when is checked, and stringlen>0

change to: StringLen(GUICtrlRead($Input1))>0...if you want only spaces to be able to enable the button as well

#region ### START Koda GUI section ###
$Form1 = GUICreate("Form1", 401, 193, 192, 124)
$Input1 = GUICtrlCreateInput("", 120, 16, 160, 21)
$Button1 = GUICtrlCreateButton("Button1", 120, 72, 160, 25)
GUICtrlSetState($Button1, $GUI_DISABLE)
$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 122, 128, 97, 17)
GUICtrlSetState($Checkbox1, $GUI_CheckED)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    If BitAnd(GUICtrlRead($Checkbox1),$GUI_CHECKED) And StringLen(StringSTripWS(GUICtrlRead($Input1),8))>0 Then
        If Not ControlCommand($Form1, "", $Button1, "IsEnabled") Then GUICtrlSetState($Button1, $GUI_ENABLE)
    Else
        If ControlCommand($Form1, "", $Button1, "IsEnabled") Then GUICtrlSetState($Button1, $GUI_DISABLE)
    EndIf

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button1
            ; do your thing
    EndSwitch
WEnd
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hello,

You just needed to split this:

If BitAnd(GUICtrlRead($Checkbox1),$GUI_UNCHECKED) = $GUI_UNCHECKED And GUICtrlRead($Input1) = ""  And $i = 1 Then

Into 2 seperate if statements:

If GUICtrlRead($Input1) = ""  And $button_enabled = 1 Then

        GUICtrlSetState($Button1, $GUI_DISABLE)
        $button_enabled = 0

    Elseif BitAnd(GUICtrlRead($Checkbox1),$GUI_UNCHECKED) = $GUI_UNCHECKED and $button_enabled = 1 Then

        GUICtrlSetState($Button1, $GUI_DISABLE)
        $button_enabled = 0

Full code:

#NoTrayIcon
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ###
$Form1 = GUICreate("Form1", 401, 193, 192, 124)
$Input1 = GUICtrlCreateInput("", 120, 16, 160, 21)
$Button1 = GUICtrlCreateButton("Button1", 120, 72, 160, 25)
GUICtrlSetState($Button1, $GUI_DISABLE)
$Checkbox1 = GUICtrlCreateCheckbox("Checkbox1", 122, 128, 97, 17)
GUICtrlSetState($Checkbox1, $GUI_CheckED)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###


$button_enabled = 1

While 1
    If GUICtrlRead($Input1) = ""  And $button_enabled = 1 Then

        GUICtrlSetState($Button1, $GUI_DISABLE)
        $button_enabled = 0

    Elseif BitAnd(GUICtrlRead($Checkbox1),$GUI_UNCHECKED) = $GUI_UNCHECKED and $button_enabled = 1 Then

        GUICtrlSetState($Button1, $GUI_DISABLE)
        $button_enabled = 0

    ElseIf $button_enabled = 0 And BitAnd(GUICtrlRead($Checkbox1),$GUI_CHECKED) = $GUI_CHECKED And GUICtrlRead($Input1) <> "" Then

            GUICtrlSetState($Button1, $GUI_ENABLE)
            $button_enabled = 1

    EndIf

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            ; do your thing
    EndSwitch
WEnd
Edited by Neutro

Share this post


Link to post
Share on other sites

I'd stick with mine :P...the above will have significant 'flashing' of the button, because he is not checking states prior to changing...also, mine has fewer blocks, and is easier to follow.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

If you tried mine before posting, you would have seen that there is no "significant flashing" at all :P

Your way of doing it is indeed shorter but I thought making only a few change to OP code would help him understand why his first code was not working :)

PS: congratulations on your 1000th post :)

Edited by Neutro

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Try this, and you should not have any button flicker.

If StringStripWS(GUICtrlRead($Input1), 8) = "" OR BitAnd(GUICtrlRead($Checkbox1),$GUI_UNCHECKED) = $GUI_UNCHECKED And BitAND(GUICtrlGetState($Button1), $GUI_ENABLE) = $GUI_ENABLE Then
 
GUICtrlSetState($Button1, $GUI_DISABLE)
 
ElseIf StringStripWS(GUICtrlRead($Input1), 8) <> "" AND BitAnd(GUICtrlRead($Checkbox1),$GUI_CHECKED) = $GUI_CHECKED And BitAND(GUICtrlGetState($Button1), $GUI_DISABLE) = $GUI_DISABLE Then
 
GUICtrlSetState($Button1, $GUI_ENABLE)
 
EndIf
Edited by AdamUL

Share this post


Link to post
Share on other sites

Hi all

Thank you, it's working

Universalist

I didn't know this method something to study thx :wacko:

Neutro

Thank you for explaining, en showing where I make the the mistake (good for my learning process)

AdamUL

Thanxs, if I fill input1 and checkbox1 is checked, button1 is enabled but when I uncheck  checkbox1, button1 is still enabled

:thumbsup:  :bye:

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Mecano,

Sorry used a wrong operator, AND, instead of OR in the first part of the If statement. I have updated my post.

The code should be work properly now.

Edited by AdamUL

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