Jump to content
Moist

Button and Checkboxes

Recommended Posts

Moist

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?

Share this post


Link to post
Share on other sites
FrancescoDiMuro
Posted (edited)

@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
  • Like 1

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Moist
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.

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@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

:)
 

  • Like 1

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro

Happy to have helped :)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Moist
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.

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
Moist

@FrancescoDiMuro

Well, if it's not possible, it's okay. I'll just create some functions that repeat in multiple cases and call them. Thanks :)

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@Moist
As I said, post your script ( if you want ), so we can see how we can help you :)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
spudw2k

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

  • Like 2

Share this post


Link to post
Share on other sites
caramen
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

IUIAutomation From Junkew WebDriver From Danp2

 

Share this post


Link to post
Share on other sites
FrancescoDiMuro

@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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
caramen
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

IUIAutomation From Junkew WebDriver From Danp2

 

Share this post


Link to post
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

×