Jump to content

Button and Checkboxes


Moist
 Share

Recommended Posts

Hello guys,

I looked everywhere but could not find exacly what was looking for. I want to have a window with a single button and 2 checkboxes. When the user presses the button, the script detect which checkboxes are pressed and will act accordingly.  I am having problems creating the Switch and If Statements. Also, I want one of the checkboxes to be pressed be default, and the user can deactivate them if he desires.

 

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

$Form=GUICreate(...

$Button=GUICtrlCreateButton(...

$Checkbox1 = GUICtrlCreateCheckbox(...

$Check1 = GUICtrlRead($Checkbox1)

$Checkbox2 = GUICtrlCreateCheckbox(...

   GUICtrlSetState(-1, $GUI_CHECKED)

$Check2 = GUICtrlRead($Checkbox2)

 

While 1

$nMsg = GUIGetMsg()
   Switch $nMsg
   Case $Button1

           If _IsChecked($Checkbox1)  AND _IsChecked($Checkbox2) Then
            MsgBox("", "1 and 2 Checked", ""))

             Sleep(1000)

            Exit

            EndIf

        If _IsChecked($Checkbox1)  AND NOT _IsChecked($Checkbox2) Then
            MsgBox("", "1 Checked", ""))

         Sleep(1000)

         Exit

            EndIf

        If NOT _IsChecked($Checkbox1)  AND _IsChecked($Checkbox2) Then
            MsgBox("", 2 Checked", ""))

        Sleep(1000)

         Exit

            EndIf

        If NOT _IsChecked($Checkbox1)  AND NOT _IsChecked($Checkbox2) Then
            MsgBox("", "0 Checked", ""))

        Sleep(1000)

         Exit

            EndIf

EndSwitch

WEnd

 

Can someone help me?

Link to comment
Share on other sites

@Moist
You can use the Select statement to do what you are trying to do :)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=
Global $frmMainForm = GUICreate("A Form", 161, 112, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApplication")
Global $chkCheckbox1 = GUICtrlCreateCheckbox("First Checkbox", 16, 15, 113, 17)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
Global $chkCheckbox2 = GUICtrlCreateCheckbox("Second Checkbox", 16, 39, 129, 17)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
Global $btnButton1 = GUICtrlCreateButton("Button", 43, 71, 75, 25)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
GUICtrlSetOnEvent(-1, "ButtonPress")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func ExitApplication()
    Exit
EndFunc

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

Func ButtonPress()

    Select
        Case _IsChecked($chkCheckbox1) And _IsChecked($chkCheckbox2)
            ConsoleWrite("Both Checkboxes are selected." & @CRLF)
        Case _IsChecked($chkCheckbox1)
            ConsoleWrite("Checkbox1 is selected." & @CRLF)
        Case _IsChecked($chkCheckbox2)
            ConsoleWrite("Checkbox2 is selected." & @CRLF)
        Case Else
            ConsoleWrite("No checkboxes selected." & @CRLF)
    EndSelect

EndFunc

 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

48 minutes ago, FrancescoDiMuro said:

@Moist
You can use the Select statement to do what you are trying to do :)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=
Global $frmMainForm = GUICreate("A Form", 161, 112, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApplication")
Global $chkCheckbox1 = GUICtrlCreateCheckbox("First Checkbox", 16, 15, 113, 17)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
Global $chkCheckbox2 = GUICtrlCreateCheckbox("Second Checkbox", 16, 39, 129, 17)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
Global $btnButton1 = GUICtrlCreateButton("Button", 43, 71, 75, 25)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
GUICtrlSetOnEvent(-1, "ButtonPress")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func ExitApplication()
    Exit
EndFunc

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

Func ButtonPress()

    Select
        Case _IsChecked($chkCheckbox1) And _IsChecked($chkCheckbox2)
            ConsoleWrite("Both Checkboxes are selected." & @CRLF)
        Case _IsChecked($chkCheckbox1)
            ConsoleWrite("Checkbox1 is selected." & @CRLF)
        Case _IsChecked($chkCheckbox2)
            ConsoleWrite("Checkbox2 is selected." & @CRLF)
        Case Else
            ConsoleWrite("No checkboxes selected." & @CRLF)
    EndSelect

EndFunc

 

Thanks, I got the checkboxes working as intended. However, I can't close the window. I have created another button to EXIT but it doesn't work. I used

GUICtrlSetOnEvent($GUI_EVENT_CLOSE, "ExitApplication") 

and 

GUISetOnEvent(-1, "ExitApplication")

right after creating the EXIT button but with no luck.

Link to comment
Share on other sites

@Moist

GUISetOnEvent() is to set a function to run when an event is fired from a GUI;
GUICtrlSetOnEvent() is to set a function to run when a control fire some events ( a click, for example ).
So, use:

GUICtrlSetOnEvent($btnExitButton, "ExitApplication") ; For a Control
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApplication")   ; For the GUI

:)
 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

55 minutes ago, FrancescoDiMuro said:

Happy to have helped :)

I have another question, though. When I use the Select function can my script only run one "Case"? Because I have, in fact, 5 checkboxes and I didn't want to make cases for all possible combinations.

Link to comment
Share on other sites

@Moist
I think not, since, if you need to do some combinations with your checkboxes (i.e.: checking 1st and 3rd checkbox will run a function, checking only 1st will run another function ), you have to separate them logically.
Post your script and let us see if we can help you more :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

You could use an array.  Here's one example:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
Global $aCheckbox[2]

#Region ### START Koda GUI section ### Form=
Global $frmMainForm = GUICreate("A Form", 161, 112, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApplication")
$aCheckbox[0] = GUICtrlCreateCheckbox("First Checkbox", 16, 15, 113, 17)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
$aCheckbox[1] = GUICtrlCreateCheckbox("Second Checkbox", 16, 39, 129, 17)
GUICtrlSetFont(-1, 10, 400, 0, "Arial")
Global $btnButton1 = GUICtrlCreateButton("Button", 43, 71, 75, 25)
GUICtrlSetFont(-1, 10, 800, 0, "Arial")
GUICtrlSetOnEvent(-1, "ButtonPress")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func ExitApplication()
    Exit
EndFunc

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

Func ButtonPress()
    For $iCheckbox = 0 to UBound($aCheckbox)-1
        If _IsChecked($aCheckbox[$iCheckbox]) Then
            ConsoleWrite("$aCheckbox[" & $iCheckbox & "] is selected." & @CRLF)
        Else
            ConsoleWrite("$aCheckbox[" & $iCheckbox & "] is NOT selected." & @CRLF)
        EndIf
    Next
EndFunc

If you wanted to get fancy, you could keep track of the checked boxes as they are checked and not have to enumerate through all of them later.  ;)

Link to comment
Share on other sites

  • 1 month later...
On 09/10/2018 at 1:04 PM, FrancescoDiMuro said:

GUISetOnEvent() is to set a function to run when an event is fired from a GUI;
GUICtrlSetOnEvent() is to set a function to run when a control fire some events ( a click, for example ).
So, use:

GUICtrlSetOnEvent($btnExitButton, "ExitApplication") ; For a Control
GUISetOnEvent($GUI_EVENT_CLOSE, "ExitApplication")   ; For the GUI

:)

@FrancescoDiMuro This mean this technic is dodging the fact that you do nothing if you are in a while ?

 

Well just so you both know it... HotKeySet also doging it. But i would like to know about GUICtrlSetOnEvent :) 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

@caramen

Basically, GUICtrlSetOnEvent() is used when you don't want to continuousely see if there is an event (captured by GUIGetMsg()).

Other events, like double click or any other event that is not "captured" from the GUICtrlSetOnEvent(), has to br captured from a WM_* handler :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

11 hours ago, FrancescoDiMuro said:

@caramen

Basically, GUICtrlSetOnEvent() is used when you don't want to continuousely see if there is an event (captured by GUIGetMsg()).

Other events, like double click or any other event that is not "captured" from the GUICtrlSetOnEvent(), has to br captured from a WM_* handler :)

Ok i got that. But you dont said me. Is it réacting as a hotkeyset ? I mean if you are actually inside a sleep. Does it register évent and do them directly instead of waiting the end of the sleep ?

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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...