Jump to content

If button is clicked then add Checkbox


Zobu
 Share

Recommended Posts

Hey Guys,

I want to add a new checkbox with its own variable every time the add button is clicked.

The added checkboxes should remain when I close the window or exit the script and when I reopen I should be able to add new checkboxes aswell.

here is what I have so far..

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <MsgBoxConstants.au3>
#include <GuiButton.au3>

 $test = GUICreate("adding test", 475, 345, 500, 175)
    $Check1 = GUICtrlCreateCheckbox("Checkbox 1", 15, 25, 300, 25)
    $Button = GUICtrlCreateButton("Add", 365, 25, 90, 20)
    $Check2 = GUICtrlCreateCheckbox("Checkbox 2", 15, 50, 300, 25)
    $Check3 = GUICtrlCreateCheckbox("Checkbox 3", 15, 75, 300, 25)

                GUICtrlSetState($Check2, $GUI_HIDE)

                GUICtrlSetState($Check3, $GUI_HIDE)

    GUISetState(@SW_SHOW)


    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ;
                Exit

            Case $Button
                GUICtrlSetPos($Check2, 15, 50, 300, 25)

                GUICtrlSetState($Check2, $GUI_SHOW)

            Case $Button
                GUICtrlSetPos($Check3, 15, 75, 300, 25)

                GUICtrlSetState($Check3, $GUI_SHOW)


            EndSwitch

            WEnd

 

Edited by Zobu
Link to comment
Share on other sites

You will need to store the number of already created checkbox into a file (like an .ini file).  so when you exit your script you can find how many CB has been created.  You also should create the CB when the button is pushed, not at beginning of the script like you did (except of course the ones that are already created).

Something like that should get you started :

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <MsgBoxConstants.au3>
#include <GuiButton.au3>

Local Const $MAX_CB = 12 ; maximum of checkboxes

Local $test = GUICreate("adding test", 475, 345, 500, 175)
Local $aCheck[$MAX_CB+1] = [1,GUICtrlCreateCheckbox("Checkbox 1", 15, 25, 300, 25)]
Local $Button = GUICtrlCreateButton("Add", 365, 25, 90, 20)
Local $iNumCB = IniRead ("Test.ini","CB","Created",1)

For $i = 2 To $iNumCB
  $aCheck[$i] = GUICtrlCreateCheckbox("Checkbox " & $i, 15, $i*25, 300, 25)
Next
$aCheck[0] = $iNumCB

GUISetState(@SW_SHOW)

While 1
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      Exit
    Case $Button
      If $aCheck[0] = $MAX_CB Then
        MsgBox ($MB_SYSTEMMODAL,"","Maximum of check boxes reach")
      Else
        $aCheck[0] += 1
        $aCheck[$aCheck[0]] = GUICtrlCreateCheckbox("Checkbox " & $aCheck[0], 15, $aCheck[0]*25, 300, 25)
        IniWrite ("Test.ini","CB","Created",$aCheck[0])
      EndIf
  EndSwitch
WEnd

 

Link to comment
Share on other sites

You could enlarge the GUI, or create a second column of check boxes, or compact them.

And, Please do not quote everything I said, just do a simple reply. It makes the thread unnecessary clumsy. 

Link to comment
Share on other sites

4 hours ago, Nine said:

You could enlarge the GUI, or create a second column of check boxes, or compact them.

And, Please do not quote everything I said, just do a simple reply. It makes the thread unnecessary clumsy. 

how to add a column with ur code? i tried it a bit myself but it keeps adding in first column after checkbox 12

 

Link to comment
Share on other sites

Simple maths :

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <MsgBoxConstants.au3>
#include <GuiButton.au3>

Local Const $MAX_ROW = 12
Local Const $MAX_COL = 3
Local Const $MAX_CB = $MAX_ROW*$MAX_COL ; maximum of checkboxes

Local $test = GUICreate("adding test", 475, 345, 500, 175)
Local $aCheck[$MAX_CB+1] = [1,GUICtrlCreateCheckbox("Checkbox 1", 15, 25, 100, 25)]
Local $Button = GUICtrlCreateButton("Add", 365, 25, 90, 20)
Local $iNumCB = IniRead ("Test.ini","CB","Created",1)

For $i = 2 To $iNumCB
  $aCheck[$i] = GUICtrlCreateCheckbox("Checkbox " & $i, 15+Floor(($i-1)/$MAX_ROW)*100, 25+Mod($i-1,$MAX_ROW)*25, 100, 25)
Next
$aCheck[0] = $iNumCB

GUISetState(@SW_SHOW)

While 1
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      Exit
    Case $Button
      If $aCheck[0] = $MAX_CB Then
        MsgBox ($MB_SYSTEMMODAL,"","Maximum of check boxes reach")
      Else
        $aCheck[0] += 1
        $aCheck[$aCheck[0]] = GUICtrlCreateCheckbox("Checkbox " & $aCheck[0], 15+Floor(($aCheck[0]-1)/$MAX_ROW)*100, 25+Mod($aCheck[0]-1,$MAX_ROW)*25, 100, 25)
        IniWrite ("Test.ini","CB","Created",$aCheck[0])
      EndIf
  EndSwitch
WEnd

 

Link to comment
Share on other sites

I would suggest to use tabs maybe. 

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

10 hours ago, Nine said:

working good thank you

last question just for future projects is there a way to show a icon instead of the checkbox text?

 

GUICtrlSetImage works, but after exiting and running again, only the number is displayed. The icon only remains with the first check box

 

Edited by Zobu
Link to comment
Share on other sites

Link to comment
Share on other sites

Hey nine,

I figured out the icon part myself, but the longer I work on the project, the more cases come to my mind.

I now want to use a button to delete and show details of a specific check box when it is checked. But then I need an array for each checkbox in my INI file and not the summary of the checkboxes or maybe not?

here is my new code now

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <MsgBoxConstants.au3>
#include <GuiButton.au3>
Global $BotAccount = GUICreate("Test", 425, 277, -1, -1)
Global $Group2 = GUICtrlCreateGroup("Test's", 5, 35, 422, 275)
GUICtrlSetFont(-1, 12, 400, 0, "Calibri")
Global $add1 = GUICtrlCreateButton("ADD", 2, 5, 80, 30)
GUICtrlSetFont(-1, 11, 800, 0, "Calibri")
Global $delete1 = GUICtrlCreateButton("DELETE", 87, 5, 80, 30)
GUICtrlSetFont(-1, 11, 800, 0, "Calibri")
Global $delete1 = GUICtrlCreateButton("DETAILS", 172, 5, 80, 30)
GUICtrlSetFont(-1, 11, 800, 0, "Calibri")
Global $close1 = GUICtrlCreateButton("CLOSE", 257, 5, 80, 30)
GUICtrlSetFont(-1, 11, 800, 0, "Calibri")

Local Const $MAX_COL = 7
Local Const $MAX_ROW = 5
Local Const $MAX_CB = $MAX_COL*$MAX_ROW; maximum of checkboxes

Local $aCheck[$MAX_CB+1] = [1,GUICtrlCreateCheckbox("Checkbox 1", 15, 55, 55, 45, $BS_ICON)]
$icon = GUICtrlSetImage(-1, "C:\Users\Nutzer\Desktop\referenzen\icon.ico", 22)
Local $iNumCB = IniRead ("Test.ini","Checkbox","",1)


For $i = 2 To $iNumCB
  $aCheck[$i] = GUICtrlCreateCheckbox("Checkbox " & $i, 15+Floor(($i-1)/$MAX_ROW)*58, 55+Mod($i-1,$MAX_ROW)*45, 55, 45, $BS_ICON)
  $icon = GUICtrlSetImage(-1, "C:\Users\Nutzer\Desktop\referenzen\icon.ico", 22)
Next

$aCheck[0] = $iNumCB

GUISetState(@SW_SHOW)

While 1
  Switch GUIGetMsg()
    Case $close1
      Exit

    Case $add1
         If $aCheck[0] = $MAX_CB Then
        MsgBox ($MB_SYSTEMMODAL,"","Maximum of boxes reach")
       Else
         $aCheck[0] += 1
        $aCheck[$aCheck[0]] = GUICtrlCreateCheckbox("Checkbox " & $aCheck[0], 15+Floor(($aCheck[0]-1)/$MAX_ROW)*58, 55+Mod($aCheck[0]-1,$MAX_ROW)*45, 55, 45, $BS_ICON)
        $icon = GUICtrlSetImage(-1, "C:\Users\Nutzer\Desktop\referenzen\icon.ico", 22)
                     IniWrite ("Test.ini","Checkbox","",$aCheck[0])
                     IniWrite ("Test.ini","icon","",$icon)
       EndIf
      Case $delete1
           If IsChecked($aCheck[0]) = 1 Then
                  IsHidden($aCheck[0])

           EndIf

  EndSwitch
WEnd

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

Func IsHidden($control)
 Return BitAnd(GUICtrlRead($control),$GUI_HIDE) = $GUI_HIDE
EndFunc

 

Link to comment
Share on other sites

@Zobu  There is multiple problems with your last script, I will not rewrite it for you as it is crucial that you learn how to program with AutoIt.  This is the most important  goal of this forum, to let you learn how to fish, and not to hand you fishes...

1- $delete1 is used both for delete and details

2- GUICtrlSetImage return 0 or 1 (see help file for it).  Why would you bothered saving that in the .ini file ?

3- You always use the same icon, why would you want to save something about it into the .ini file ?  (see point 2) 

4- GUICtrlRead only returns check or uncheck (nothing else, you do not need the BitAnd stuff)

5- As stated on point 4, GUICtrlRead will not tell you if it hidden or not.  GUICtrlGetState will : see help file for it (in that case BitAnd is required)

6- Your Case $delete1 does not make sense.  You will need to loop thru all checkboxes to see if some are checked or not

7- To delete a control use GUICtrlDelete (see help file for it).  But if you start deleting controls, you will need to rearrange all your GUI.  The array needs then to be modified accordingly, which can become a tad more complex.  I am not sure you are ready for it now.

Edited by Nine
Link to comment
Share on other sites

 

I don't ask you to write me the full script, but I'm a better learner when I have examples and I can't find anything that suits my purpose. Only parts of the script or forum posts with what I'm looking for would help

 

Link to comment
Share on other sites

You can use the key part from the iniwrite, to store individual states of the check boxes.

Add to your script, just after the IniWrite ("Test.ini","Checkbox","",$aCheck[0]) something like:

IniWrite ("Test.ini","Checkbox",$aCheck[$aCheck[0]],IsChecked($aCheck[$aCheck[0]]))

In the same way you can store the hidden state of the checkbox, you only need to add a keyword for the key, something like this, (a crude example)

for $forloop=1 to $MAX_CB
IniWrite ("Test.ini","Checkbox",$aCheck[$forloop] & "_hidden",IsHidden($aCheck[$forloop]))
Next

 

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

This is how you can hide it with a button.

Case $delete1

            For $x = 1 To $aCheck[0]
                If IsChecked($aCheck[$x]) = True Then
                    If IsHidden($aCheck[$x]) = False Then
                        GUICtrlSetState($aCheck[$x], $GUI_HIDE)
                    EndIf
                EndIf
            Next

btw, your last script has a double button variable definition:

Global $delete1 = GUICtrlCreateButton("DELETE", 87, 5, 80, 30)
GUICtrlSetFont(-1, 11, 800, 0, "Calibri")
Global $delete1 = GUICtrlCreateButton("DETAILS", 172, 5, 80, 30)

 

Edited by Dan_555

Some of my script sourcecode

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

×
×
  • Create New...