AutoIt GUICtrlSetState Function

The GUICtrlSetState Function is used to control the state of GUI elements.

3453 views

Edited: 2017-02-10 21:41

The AutoIt GUICtrlSetState Function is used to change the state of GUI elements. An example would be when pressing a button that triggers a function, disabling the button while the function is running can avoid confusion.

Returns 1 on success, and 0 on failure.

Parameters

Control IDThe ID of the control to be changed, this is often saved in a variable.
StateRefer to the state-table below this table.
$GUI_UNCHECKEDWill uncheck a Radio, Checkbox or ListViewItem.
$GUI_CHECKEDWill check a Radio, Checkbox or ListViewItem.
$GUI_INDETERMINATEA Checkbox with the tristate attribute will be greyed.
$GUI_AVISTARTAn AVI control will start playing.
$GUI_AVISTOPAn AVI control will stop playing.
$GUI_AVICLOSEAn AVI control will stop playing, and release the source file.
$GUI_DROPACCEPTEDControl will accept drag and drop of files, or other GUI controls.
$GUI_NODROPACCEPTEDControl will not accept drag and drop.
$GUI_SHOWWill make a hidden control become visible.
$GUI_HIDEWill hide a control entirely.
$GUI_ENABLEEnables a control for use.
$GUI_DISABLEDisables a control from use by graying it out.
$GUI_NOFOCUSControl will lose focus.
$GUI_FOCUSControl will be given focus.
$GUI_DEFBUTTONControl will be set as default button on the window.
$GUI_EXPANDTreeViewItem will expand its child items.
$GUI_ONTOPControl will be given the ontop attribute, making it show on top of other elements (zOrdering).

States can be combined, I.e. $GUI_DISABLE + $GUI_HIDE will set the control in an disabled and hidden state.

Example

The below example scripts creates a GUI with Two Buttons, then lets you control the availability of the second button by pressing the first.

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

MainGUI()

Func MainGUI()
  Local $Button1, $Button2, $msg
  GUICreate("My GUI Window Title")

  Opt("GUICoordMode", 2)
  $Button1 = GUICtrlCreateButton("Button 1", 10, 30, 100)
  $Button2 = GUICtrlCreateButton("Button 2", 0, -1)

  GUISetState()
  GUICtrlSetState($Button2, $GUI_DISABLE)


  ; Run the GUI until the window is closed
  While 1
    $msg = GUIGetMsg()
    Select
     Case $msg = $GUI_EVENT_CLOSE
       ExitLoop
     Case $msg = $Button1
       GUICtrlSetState($Button1, $GUI_DISABLE)
       GUICtrlSetState($Button2, $GUI_ENABLE)
       MsgBox(0, 'Button 1', 'Button 1 was pressed')
     Case $msg = $Button2
       GUICtrlSetState($Button1, $GUI_ENABLE)
       GUICtrlSetState($Button2, $GUI_DISABLE)
       MsgBox(0, 'Button 2', 'Button 2 was pressed')
    EndSelect
  WEnd
EndFunc

Links

  1. GUICtrlSetState - autoitscript.com

Tell us what you think:

  1. How to create a list of selectable items with AutoIt.
  2. Tutorial on how to make GUIs using the AutoIt scripting language.
  3. How to disable and enable AutoIt GUI elements using GUICtrlSetState.
  4. How to interrupt running functions and handle system events in AutoIt GUI scripting.
  5. Tutorial on how to add images to AutoIt GUIs while maintaining aspect ratio.

More in: AutoIt GUIs