Jump to content

Disable buttons until checkbox is checked


Go to solution Solved by ahmet,

Recommended Posts

Posted

Is there a way to disable all buttons in a window until a checkbox is pressed? I've tried the following, but the buttons are still responsive, but don't trigger until after the checkbox is checked. I'm looking to have the buttons disabled (perhaps even greyed out or something of the like) to where they're 100% unresponsive until after the checkbox is checked. Thanks in advance.

While 1
    If BitAND(GUICtrlRead($idCheckBox_Accept),$GUI_Checked) Then
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Cancel
                ConsoleWrite("You clicked the [Cancel] button.")
                ExitLoop
            Case $idButton_Next
                ConsoleWrite("You clicked the [Next] button.")
                ExitLoop
        EndSwitch
    EndIf
WEnd

 

  • Solution
Posted

Here is an example. It is a modified example from help file. If you have a lot of buttons you have to disable and enable them manually, like I did in my example. If you have any questions about the code feel free to ask.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 300, 200)

    ; Create a checkbox control.
    Local $idCheckbox = GUICtrlCreateCheckbox("Standard Checkbox", 10, 10, 185, 25)
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)
    Local $idTestButton=GUICtrlCreateButton("Button with changable state",10,40);we will change state of this button
    GUICtrlSetState(-1,$GUI_DISABLE)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop

            Case $idCheckbox
                If _IsChecked($idCheckbox) Then
                    GUICtrlSetState($idTestButton,$GUI_ENABLE)
                Else
                    GUICtrlSetState($idTestButton,$GUI_DISABLE)
                EndIf
            Case $idTestButton
                MsgBox(0,"Click","I am clickable")
        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

 

Posted (edited)

You can group up multiple buttons and save their IDs into an array when each button it's created. Then you can change their state together. Here it's a modified version of ahmet.

#include <GUIConstantsEx.au3>

Local $acButtons[10]

Local $hGUI = GUICreate("Example", 300, 350)
Local $cCheckbox = GUICtrlCreateCheckbox("Standard Checkbox", 10, 10, 185, 25)
; Let's create a bunch of buttons
For $Index = 0 To UBound($acButtons) - 1
    $acButtons[$Index] = GUICtrlCreateButton("Button with changable state", 10, $Index * 25 + 40)
    GUICtrlSetState($acButtons[$Index], $GUI_DISABLE)
Next
GUISetState(@SW_SHOW, $hGUI)

While True
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cCheckbox
            ButtonsSetState(_IsChecked($cCheckbox))
        Case $acButtons[0] To $acButtons[UBound($acButtons) - 1]
            MsgBox(0, "Click", "You clicked button " & $iMsg - $acButtons[0])
    EndSwitch
WEnd

Func ButtonsSetState($bEnable = True)
    For $Index = 0 To UBound($acButtons) - 1
        GUICtrlSetState($acButtons[$Index], $bEnable ? $GUI_ENABLE : $GUI_DISABLE)
    Next
EndFunc

Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

 

Edited by Andreik

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...