Jump to content

[SOLVED] Enable Checkbox only when another check box is checked


Recommended Posts

I've read the help file, but can't find a solution.

How to enable a (disabled) checkbox only when another checkbox has been checked?

Once "Cats?" checkbox has been checked, I want the Label "Number of Cats" to be enabled and allow numeric-only input of Input Box $InputCatNo

After inputting a number and pressing the Go button, I want to run notepad and send the inputted number to notepad. Thank you in advance for advice or pointing me to a help file.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Math.au3>
#include <GUIConstantsEx.au3>
#include <Misc.au3>

Global $InputCatNo, $InputCatNo2
While 1
    _Animal()
    Run("notepad.exe")
    WinWaitActive("Untitled - Notepad")
    Send($InputCatNo2)
ExitLoop
WEnd

Func _Animal()
$aAnimal = GUICreate("Animals", 342, 273, 509, 166)

$CBcCats = GUICtrlCreateCheckbox("Cats?", 115, 25, 97, 25)
GUICtrlSetFont(-1, 16, 400, 0, "Calibri")
$LblCat=GUICtrlCreateLabel("Number of Cats", 32, 54, 113, 23, $SS_RIGHT)
GUICtrlSetFont(-1, 12, 400, 0, "Calibri")
        GUICtrlSetstate($LblCat,$GUI_DISABLE)
$InputCatNo = GUICtrlCreateInput("", 152, 54, 137, 23, BitOR($GUI_SS_DEFAULT_INPUT,$ES_NUMBER))
GUICtrlSetFont(-1, 10, 400, 0, "Calibri")
    GUICtrlSetstate($InputCatNo,$GUI_DISABLE)

$BTNGo = GUICtrlCreateButton("GO", 104, 176, 97, 57)
GUICtrlSetFont(-1, 24, 400, 0, "Calibri")

GUISetState(@SW_SHOW)
    $msg = GUIGetMsg()

    Switch GUIGetMsg()
        Case GUICtrlRead($CBcCats) = $GUI_CHECKED
            GUICtrlSetState($LblCat,$GUI_ENABLE)
            GUICtrlSetState($InputCatNo,$GUI_ENABLE)
                If GUICtrlRead($InputCatNo) <> "" Then
                    $InputCatNo2 = GUICtrlRead($InputCatNo)
                Else
                    $InputCatNo2 = ""
                EndIf
    EndSwitch

    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $BTNGo
                ExitLoop
        EndSelect
    WEnd

    Select
        Case $msg = $BTNGo
            GUISetState(@SW_HIDE)
    EndSelect

EndFunc ;=> _Animal
Edited by stealth
Solved
Link to comment
Share on other sites

  • Moderators

stealth,

How to enable a (disabled) checkbox only when another checkbox has been checked?

I can only see one checkbox - but if you mean the input then something like this should work:

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

_Animal()

Func _Animal()

    $aAnimal = GUICreate("Animals", 342, 273, 509, 166)

    $CBcCats = GUICtrlCreateCheckbox("Cats?", 115, 25, 97, 25)
    $LblCat = GUICtrlCreateLabel("Number of Cats", 32, 54, 113, 23, $SS_RIGHT)
    GUICtrlSetState($LblCat, $GUI_DISABLE)
    $InputCatNo = GUICtrlCreateInput("", 152, 54, 137, 23, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
    GUICtrlSetState($InputCatNo, $GUI_DISABLE)

    $BTNGo = GUICtrlCreateButton("GO", 104, 176, 97, 57)

    GUISetState(@SW_SHOW)

    While 1

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                GUIDelete($aAnimal)
                Return
            Case $BTNGo
                $iCount = GUICtrlRead($InputCatNo)
                If $iCount Then
                    MsgBox($MB_SYSTEMMODAL, "Cats required", $iCount)
                Else
                    MsgBox($MB_SYSTEMMODAL, "Oh dear", " No cats required")
                EndIf
            Case $CBcCats
                If GUICtrlRead($CBcCats) = $GUI_CHECKED Then
                    GUICtrlSetState($LblCat, $GUI_ENABLE)
                    GUICtrlSetState($InputCatNo, $GUI_ENABLE)
                Else
                    GUICtrlSetState($LblCat, $GUI_DISABLE)
                    GUICtrlSetState($InputCatNo, $GUI_DISABLE)
                EndIf
        EndSwitch

    WEnd

EndFunc   ;==>_Animal

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you, Melba.

I meant  "Enable GUI only when checkbox is checked" in topic and "How to enable a (disabled) GUI only when checkbox has been checked?" as my question.

But, you pointed out where my logic was wrong. Thank you!!

Link to comment
Share on other sites

The variable $iCount only gets sent to notepad when the red X is clicked, and clicking the GO button takes no action. Why? What am I doing wrong?

I want to pass the $InputCatNo as a variable--could be $iCount or another variable. Thank you in advance!

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
;#include <MsgBoxConstants.au3>
Global $iCount, $InputCatNo

While 1
    _Animal()
    Run("notepad.exe")
    WinWaitActive("Untitled - Notepad")
    Send($iCount & "{ENTER}")
ExitLoop
WEnd

Func _Animal()
$aAnimal = GUICreate("Animals", 342, 273, 509, 166)
$CBcCats = GUICtrlCreateCheckbox("Cats?", 115, 25, 97, 25)
$LblCat = GUICtrlCreateLabel("Number of Cats", 32, 54, 113, 23, $SS_RIGHT)
GUICtrlSetState($LblCat, $GUI_DISABLE)
$InputCatNo = GUICtrlCreateInput("", 152, 54, 137, 23, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
GUICtrlSetState($InputCatNo, $GUI_DISABLE)
$BTNGo = GUICtrlCreateButton("GO", 104, 176, 97, 57)

GUISetState(@SW_SHOW)
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIDelete($aAnimal)
            Return
        Case $BTNGo
            $iCount = GUICtrlRead($InputCatNo)
            If $iCount Then
                $iCount = $iCount
            Else
                $iCount= ""
            EndIf
        Case $CBcCats
            If GUICtrlRead($CBcCats) = $GUI_CHECKED Then
                GUICtrlSetState($LblCat, $GUI_ENABLE)
                GUICtrlSetState($InputCatNo, $GUI_ENABLE)
            Else
                GUICtrlSetState($LblCat, $GUI_DISABLE)
                GUICtrlSetState($InputCatNo, $GUI_DISABLE)
            EndIf
    EndSwitch
WEnd
EndFunc   ;==>_Animal

 

Link to comment
Share on other sites

This works for me, as you have stated you would like it to work:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
;#include <MsgBoxConstants.au3>
Global $iCount, $InputCatNo


_Animal()

Func _Animal()
    $aAnimal = GUICreate("Animals", 342, 273, 509, 166)
    $CBcCats = GUICtrlCreateCheckbox("Cats?", 115, 25, 97, 25)
    $LblCat = GUICtrlCreateLabel("Number of Cats", 32, 54, 113, 23, $SS_RIGHT)
    GUICtrlSetState($LblCat, $GUI_DISABLE)
    $InputCatNo = GUICtrlCreateInput("", 152, 54, 137, 23, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
    GUICtrlSetState($InputCatNo, $GUI_DISABLE)
    $BTNGo = GUICtrlCreateButton("GO", 104, 176, 97, 57)

    GUISetState(@SW_SHOW)
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Return
            Case $BTNGo
                $iCount = GUICtrlRead($InputCatNo)
                If $iCount Then
                    $iCount = $iCount
                Else
                    $iCount = ""
                EndIf
                Run("notepad.exe")
                WinWaitActive("Untitled - Notepad")
                Send($iCount & "{ENTER}")
            Case $CBcCats
                If GUICtrlRead($CBcCats) = $GUI_CHECKED Then
                    GUICtrlSetState($LblCat, $GUI_ENABLE)
                    GUICtrlSetState($InputCatNo, $GUI_ENABLE)
                Else
                    GUICtrlSetState($LblCat, $GUI_DISABLE)
                    GUICtrlSetState($InputCatNo, $GUI_DISABLE)
                EndIf
        EndSwitch
    WEnd
EndFunc   ;==>_Animal

All good? :)

Edited by MikahS
Took out GUIDelete, as $GUI_EVENT_CLOSE is the closing of the GUI anyway.

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Thank you for your suggestion, MilkahS and all--you've pointed me in the right direction! 

I have several dozen 'animals' and input boxes for count of animals and  need to pass the variables to outside the Function to do with the counts later. I got the flickering inputboxes and then found Walkillon's suggestion: https://www.autoitscript.com/forum/topic/149208-flickering-input-box/?do=findComment&comment=1062398

Next question. Say a user checks "Cats?" checkbox, inputs a number, but decides to push Red X instead of "Go"; how do I erase the number and send nothing to notepad? Something like If Red X is pressed then $iCount = ""   ?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
;#include <MsgBoxConstants.au3>

AutoItSetOption("SendKeyDelay", 50)
;Opt("SendKeyDelay", 5)


Global $iCount, $InputCatNo

While 1
    _Animal()
    Run("notepad.exe")
    WinWaitActive("Untitled - Notepad")
    If $iCount <> "" Then Send($iCount & "{ENTER}")
    ExitLoop
WEnd

Func _Animal()
    $aAnimal = GUICreate("Animals", 342, 273, 509, 166)
    $CBcCats = GUICtrlCreateCheckbox("Cats?", 115, 25, 97, 25)
    $LblCat = GUICtrlCreateLabel("Number of Cats", 32, 54, 113, 23, $SS_RIGHT)
    GUICtrlSetState($LblCat, $GUI_DISABLE)
    $InputCatNo = GUICtrlCreateInput("", 152, 54, 137, 23, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
    GUICtrlSetState($InputCatNo, $GUI_DISABLE)
    $BTNGo = GUICtrlCreateButton("GO", 104, 176, 97, 57)

    GUISetState(@SW_SHOW)
    GUISetState()
    $msg = 0
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $BTNGo
                ExitLoop
        EndSelect
        Switch $msg
            Case $CBcCats
            If GUICtrlRead($CBcCats) = $GUI_CHECKED Then
                    GUICtrlSetState($LblCat, $GUI_ENABLE)
                    GUICtrlSetState($InputCatNo, $GUI_ENABLE)
                ElseIf GUICtrlRead($CBcCats) = $GUI_UNCHECKED Then
                    GUICtrlSetState($LblCat, $GUI_DISABLE)
                    GUICtrlSetState($InputCatNo, $GUI_DISABLE)
                EndIf
        EndSwitch
    WEnd

    $iCount = GUICtrlRead($InputCatNo)
    If $iCount Then
        $iCount = $iCount
    Else
        $iCount = ""
    EndIf

    Select
        Case $msg = $BTNGo
            GUISetState(@SW_HIDE)
    EndSelect
EndFunc   ;==>_Animal
Link to comment
Share on other sites

I added some comments to show what I did:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
;#include <MsgBoxConstants.au3>

AutoItSetOption("SendKeyDelay", 50)
;Opt("SendKeyDelay", 5)


Global $iCount, $InputCatNo

While 1
    _Animal()
    Run("notepad.exe")
    WinWaitActive("Untitled - Notepad")
    If $iCount <> "" Then Send($iCount & "{ENTER}")
    ExitLoop
WEnd

Func _Animal()
    $aAnimal = GUICreate("Animals", 342, 273, 509, 166)
    $CBcCats = GUICtrlCreateCheckbox("Cats?", 115, 25, 97, 25)
    $LblCat = GUICtrlCreateLabel("Number of Cats", 32, 54, 113, 23, $SS_RIGHT)
    GUICtrlSetState($LblCat, $GUI_DISABLE)
    $InputCatNo = GUICtrlCreateInput("", 152, 54, 137, 23, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER))
    GUICtrlSetState($InputCatNo, $GUI_DISABLE)
    $BTNGo = GUICtrlCreateButton("GO", 104, 176, 97, 57)

    GUISetState(@SW_SHOW)
    $msg = 0
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $BTNGo
                $iCount = GUICtrlRead($InputCatNo) ; read the input
                If $iCount Then ; if we have something in the box
                    $iCount = $iCount ; set the count
                Else
                    $iCount = "" ; if there is nothing, then set $iCount to nothing
                EndIf
                ContinueLoop ; keep going in the loop
            Case $msg = $GUI_EVENT_CLOSE ; we need to make sure we are accounting for the red "x"
                $iCount = "" ; set the count to nothing, since we did not want to go
                ExitLoop ; exit the entire loop
        EndSelect
        Switch $msg
            Case $CBcCats
                If GUICtrlRead($CBcCats) = $GUI_CHECKED Then
                    GUICtrlSetState($LblCat, $GUI_ENABLE)
                    GUICtrlSetState($InputCatNo, $GUI_ENABLE)
                ElseIf GUICtrlRead($CBcCats) = $GUI_UNCHECKED Then
                    GUICtrlSetState($LblCat, $GUI_DISABLE)
                    GUICtrlSetState($InputCatNo, $GUI_DISABLE)
                EndIf
        EndSwitch
    WEnd

    Select
        Case $msg = $BTNGo
            GUISetState(@SW_HIDE)
    EndSelect
EndFunc   ;==>_Animal

Glad to help. ^_^

Edited by MikahS
comment

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Ah-ha! Thank you, All!! I think I got it (for now) ;). Assigned $vCount to not zero and Go button; $iCount to red 'x'

Here is only an label and input box that I'll build upon.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
;#include <MsgBoxConstants.au3>

AutoItSetOption("SendKeyDelay", 50)
;Opt("SendKeyDelay", 5)


Global $iCount, $InputCatNo, $vCount

While 1
    _Animal()
    Run("notepad.exe")
    WinWaitActive("Untitled - Notepad")
    If $vCount <> "" Then Send($vCount & "{ENTER}") ;send $vCount only if a number > 0 is inputted
    ExitLoop ;exit While1 Loop
WEnd

Func _Animal()
$aAnimal = GUICreate("Animals", 342, 273, 509, 166)
$LblCat = GUICtrlCreateLabel("Number of Cats", 32, 54, 113, 23, $SS_RIGHT)
$InputCatNo = GUICtrlCreateInput("", 152, 54, 137, 23, BitOR($GUI_SS_DEFAULT_INPUT, $ES_NUMBER)) ;allow only numbers to be inputted
$BTNGo = GUICtrlCreateButton("GO", 104, 176, 97, 57)

GUISetState(@SW_SHOW)
GUISetState()
$msg = 0
While $msg <> $GUI_EVENT_CLOSE ; as long as red 'x' is not clicked, loop
    $msg = GUIGetMsg() ; polls GUI to see if any events have occurred
    $iCount = GUICtrlRead($InputCatNo) ; read the input
    If $iCount <> 0 Then ; if we have something in the box not zero
        $vCount = $iCount ; set $vCount to $iCount
    Else
        $iCount = "" ; if there is nothing, then set $iCount to nothing
    EndIf
    Select
        Case $msg = $BTNGo ;when GO is clicked
            GUISetState(@SW_HIDE);  hide the GUI
            ExitLoop; exit the entire loop
        Case $msg = $GUI_EVENT_CLOSE ; we need to make sure we are accounting for the red "x"
            $vCount = "" ; set the $vCount to nothing, since we did not want to go
            ExitLoop ; exit the entire loop
    EndSelect
WEnd
EndFunc   ;==>_Animal

 

Link to comment
Share on other sites

My pleasure. ;)

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

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

×
×
  • Create New...