Jump to content

How to Check whether a CheckBox is Checked or not


Recommended Posts

Hi,

I have a checkbox in my application. I want to check whether the checkbox is checked or not.

I tried with GUICtrlRead(), but I didn't get any output. My checkbox has the following Inforamtion

ClassName

Caption

TypeName

ID

style

Using the above properties how can I resolve my question

Thanks in Advance.

Link to comment
Share on other sites

Hi,

I have a checkbox in my application. I want to check whether the checkbox is checked or not.

I tried with GUICtrlRead(), but I didn't get any output. My checkbox has the following Inforamtion

ClassName

Caption

TypeName

ID

style

Using the above properties how can I resolve my question

Thanks in Advance.

You find the state of the checkbox. If the state include $GUI_CKECKED then it is checked.

$IsChecked = GuiCtrlRead($ChkBox1) = $GUI_CHECKED

Welcome to the AutoIt forums sagirajuramaraju :)

EDIT: Corrected wrong advice

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

You find the state of the checkbox. If the state include $GUI_CKECKED then it is checked.

$IsChecked = BitAnd(GuiCtrlGetState($ChkBox1),$GUI_CHECKED)

Welcome to the AutoIt forums sagirajuramaraju :)

Can you give descriptive example because still I didn't get that result. Can you know how can I know the checkbox ObjectType

Link to comment
Share on other sites

#include <GuiConstants.au3>

Global $hGUI
Global $CheckBox

$hGUI = GUICreate('Test', 100, 50)
$CheckBox = GUICtrlCreateCheckbox('Unchecked', 10, 15, 80, 23)

GUISetState()
While 1
    Switch GUIGetMsg()
        Case $CheckBox
            If GUICtrlRead($CheckBox) = $GUI_CHECKED Then
                GUICtrlSetData($CheckBox, 'Checked')
            Else
                GUICtrlSetData($CheckBox, 'Unchecked')
            EndIf
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

GUIDelete()
Exit

Link to comment
Share on other sites

Can you give descriptive example because still I didn't get that result. Can you know how can I know the checkbox ObjectType

Apologies, GuiCtrlGetState doesn't return with the checked state information, as the help says

Function GUICtrlGetStateFunction GUICtrlGetStateAs opposed to GUICtrlRead this function returns ONLY the state of a control enabled/disabled/hidden/show/dropaccepted

whereas GuiCtrlRead only return the checked state of a check box, as in Authenticity's example.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Here is correct way:

#include <GuiConstants.au3>

Global $hGUI
Global $CheckBox

$hGUI = GUICreate('Test', 100, 50)
$CheckBox = GUICtrlCreateCheckbox('Unchecked', 10, 15, 80, 23)

GUISetState()
While 1
    Switch GUIGetMsg()
        Case $CheckBox
            If IsChecked($CheckBox) Then
                GUICtrlSetData($CheckBox, 'Checked')
            Else
                GUICtrlSetData($CheckBox, 'Unchecked')
            EndIf
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

GUIDelete()
Exit

Func IsChecked($control)
 Return BitAnd(GUICtrlRead($control),$GUI_CHECKED) = $GUI_CHECKED
EndFunc

This works fine also in case checkbox has other styles than checked/unchecked at the same time - for example disabled,...

Edited by Zedna
Link to comment
Share on other sites

Here is correct way:

#include <GuiConstants.au3>

Global $hGUI
Global $CheckBox

$hGUI = GUICreate('Test', 100, 50)
$CheckBox = GUICtrlCreateCheckbox('Unchecked', 10, 15, 80, 23)

GUISetState()
While 1
    Switch GUIGetMsg()
        Case $CheckBox
            If IsChecked($CheckBox) Then
                GUICtrlSetData($CheckBox, 'Checked')
            Else
                GUICtrlSetData($CheckBox, 'Unchecked')
            EndIf
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

GUIDelete()
Exit

Func IsChecked($control)
 Return BitAnd(GUICtrlRead($control),$GUI_CHECKED) = $GUI_CHECKED
EndFunc

This works fine also in case checkbox has other styles than checked/unchecked at the same time - for example disabled,...

No, I think you are wrong Zedna. I used to think that but I tried GuiCtrlRead with disabled and enabled check boxes and it still gives the correct result.

#include <GuiConstantsEx.au3>

Global $hGUI
Global $CheckBox, $CheckEnable, $Btn

$hGUI = GUICreate('Test', 170, 250)
$CheckBox = GUICtrlCreateCheckbox('Unchecked', 10, 15, 80, 23)
$Btn = GUICtrlCreateButton("read checkbox state", 5, 90, 130, 22)
$CheckEnable = GUICtrlCreateCheckbox("disabled checkbox state ", 10, 150, 120, 22)
GUISetState()
$chkEnabled = True
While 1
    Switch GUIGetMsg()
        Case $CheckBox
            If GUICtrlRead($CheckBox) = $GUI_CHECKED Then
                GUICtrlSetData($CheckBox, 'Checked')
            Else
                GUICtrlSetData($CheckBox, 'Unchecked')
            EndIf
        Case $Btn
            $IsChecked = GuiCtrlRead($CheckBox) = $GUI_CHECKED
            ConsoleWrite($IsChecked & ', ' & Hex($GUI_CHECKED) & @CRLF)
            If GuiCtrlRead($CheckBox) = $GUI_UNCHECKED Then
                MsgBox(262144, "check box is", 'UnChecked')
            Else
                MsgBox(262144, "check box is", 'checked')
            EndIf

        Case $CheckEnable
            ConsoleWrite("check en" & @CRLF)
            $ChkEnabled = Not $ChkEnabled
            If $ChkEnabled Then
                GUICtrlSetState($CheckBox, $GUI_ENABLE)
                GUICtrlSetData($CheckEnable, "disabled checkbox state ")
            Else
                GUICtrlSetData($CheckEnable, "enabled checkbox state ")
                GUICtrlSetState($CheckBox, $GUI_DISABLE)
            EndIf

        Case $GUI_EVENT_CLOSE
            Exit;Loop
    EndSwitch
WEnd
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I agree with you people, but here all the examples for new checkbox (that we need to create first then checking the property). That is not my question I have already a checkbox, I don't want to use GUICtrlCreateCheckbox, just I want to know the property of the existing checkbox

No, I think you are wrong Zedna. I used to think that but I tried GuiCtrlRead with disabled and enabled check boxes and it still gives the correct result.

#include <GuiConstantsEx.au3>

Global $hGUI
Global $CheckBox, $CheckEnable, $Btn

$hGUI = GUICreate('Test', 170, 250)
$CheckBox = GUICtrlCreateCheckbox('Unchecked', 10, 15, 80, 23)
$Btn = GUICtrlCreateButton("read checkbox state", 5, 90, 130, 22)
$CheckEnable = GUICtrlCreateCheckbox("disabled checkbox state ", 10, 150, 120, 22)
GUISetState()
$chkEnabled = True
While 1
    Switch GUIGetMsg()
        Case $CheckBox
            If GUICtrlRead($CheckBox) = $GUI_CHECKED Then
                GUICtrlSetData($CheckBox, 'Checked')
            Else
                GUICtrlSetData($CheckBox, 'Unchecked')
            EndIf
        Case $Btn
            $IsChecked = GuiCtrlRead($CheckBox) = $GUI_CHECKED
            ConsoleWrite($IsChecked & ', ' & Hex($GUI_CHECKED) & @CRLF)
            If GuiCtrlRead($CheckBox) = $GUI_UNCHECKED Then
                MsgBox(262144, "check box is", 'UnChecked')
            Else
                MsgBox(262144, "check box is", 'checked')
            EndIf

        Case $CheckEnable
            ConsoleWrite("check en" & @CRLF)
            $ChkEnabled = Not $ChkEnabled
            If $ChkEnabled Then
                GUICtrlSetState($CheckBox, $GUI_ENABLE)
                GUICtrlSetData($CheckEnable, "disabled checkbox state ")
            Else
                GUICtrlSetData($CheckEnable, "enabled checkbox state ")
                GUICtrlSetState($CheckBox, $GUI_DISABLE)
            EndIf

        Case $GUI_EVENT_CLOSE
            Exit;Loop
    EndSwitch
WEnd
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...