Jump to content

If button is clicked then add Checkbox


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

Show the code you attempted to, we shall see what is your problem.

To do a simple reply go at the bottom of the thread, there is a reply box, use that, thank you.

Link to post
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 post
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 post
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 post
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
Link to post
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
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By roeselpi
      hello again,
      it has been a long time since i have been here and a long time since i last used autoit. ever so often when the time allows me to, then i follow up on an idea that i had a long time ago. i have done all the work on paper but now it is up to writing it in autoit and i keep stumbling over many little issues here and there. sometimes after a few days i will try again and get a step further but sometimes it just will not help no matter how long i try and think about a solution. for most of you it will be the basics but for me it is not all that easy, but at least i give it a try.
      right, down to business:
      here is my code:
      #include <MsgBoxConstants.au3> #include <StringConstants.au3> #include <Array.au3> #include <String.au3> ; ; PART 1: define replacements and check with msgbox ; Global $y, $z $y = "Yes" $z = "No" MsgBox(0,"replacements", $y & @CRLF & $z) ;the replacements in a message box ; ; PART 2: set the texts and check via console and msgbox ; Global $my1string = "abab" ;the first specified text MsgBox(0,"my1string", $my1string) ;the message box to output the first specified text Global $my2string = "icic" ;the second specified text MsgBox(0,"my2string", $my2string) ;the message box to output the second specified text ; ; PART 3: transform the strings to individual arrays ; $my1array = StringSplit($my1string, "") $my1array[0] = "" _ArrayDelete($my1array, 0) _ArrayDisplay($my1array, "my1array") ;the display of the first specified array $my2array = StringSplit($my2string, "") $my2array[0] = "" _ArrayDelete($my2array, 0) _ArrayDisplay($my2array, "my2array") ;the display of the first specified array ; ; PART 4: create an empty array for filling ; Global $OutputArray[4] $OutputArray[0] = "" _ArrayDisplay($OutputArray, "OutputArray") ;the display of the first specified array ; ; PART 5: compare & fill empty OutputArray with data after evaluation ; Global $i, $j, $k For $i = 0 to UBound($my1array) -1 For $j = 0 to UBound($my2array) -1 For $k = 0 to UBound($OutputArray) -1 If $my1array[$i] = "a" And $my2array[$j] = "i" Then $OutputArray[$k] = $y Else $OutputArray[$k] = $z EndIf Next Next Next _ArrayDisplay($OutputArray, "OutputArray") ;the display of the Newly filled Array In "Part 2" i make a string that is converted to an array in "Part 3" ... Now, I know that "a" and "i" are always in the exact same spot in both arrays and so i wanted to compare this and make a further array to document my findings by saying "yes" or "no" ... however my new array keeps saying just "no" allthough i can clearly see and know that it should say:
      yes no yes no my guess is that there is something wrong within my for-loops and that the counting is somehow "off" i guess that when the first for-loop is finished it reaches the second whilst the second for-loop is checking the first which would explain why it always says "no" instead of seeing the obvious.
      so my question would be: what is wrong with my for-loop? or where am i making an error that ultimately gives me the wrong results?
      help is much appreciated.
      kind regards
      roeselpi
       
       
      PS: sorry for my not so great english spelling ... stupid german sitting here trying out intermediate english skills.
    • By MarcoMonte
      Hello Everyone,
      Hoping someone can help me,
      I am totally stuck on this problem,

      I cannot reach the textarea and the button in a maps page that I get by clicking on a button in a previus page, the only way I can interact with this is using tab and mouseclick but this is a danger method as  you know.

      I get the handle by using: $Posizione = _IEAttach("Ricerca Indirizzo su mappa")

      then I tried a lot of things but nothing works, can someone give me a hint?
      --------------------
      WinWait("Ricerca Indirizzo su mappa")
      sleep(1000)
      $Posizione = _IEAttach("Ricerca Indirizzo su mappa")
      WinSetState ("Ricerca Indirizzo su mappa", "", @SW_MAXIMIZE  )
      WinActivate("Ricerca Indirizzo su mappa")
      sleep(500)
      ;~ $oForm = _IEFormGetObjByName($Posizione, "form1");Punta il Form
      $oText = _IEFormElementGetObjByName($Posizione,"indirizzo")
      $oText = _IEFormElementGetObjByName($Posizione,"writeAddress")

      ;~ $LenteTestoMess = _IEFormElementGetValue($oText);Inserisce il PrimoRepertorio nella variabile
      ;~ _IELinkClickByText($Posizione, $oForm);Clicca su Aggiorna ed invia il form
      _IEFormElementSetValue ($oText,"via Ravenna 12" );Completamento campo testo
      ----------------------------------------------------
      Mappa.odt Ricerca Indirizzo su mappa.htm
    • By ozymandius257
      I'm trying to check the status of the checkbox for sharing a folder that you see when you right-click on a  folder, go to the sharing tab, then click on Advanced Sharing.

      The check box is labelled Share this folder, and looking at it using AutoIt window info, I get the following information..
      >>>> Control <<<<
      Class:    Button
      Instance:    1
      ClassnameNN:    Button1
      Name:    
      Advanced (Class):    [CLASS:Button; INSTANCE:1]
      ID:    1020
      Text:    &Share this folder
      Position:    15, 16
      Size:    306, 13
      ControlClick Coords:    57, 4
      Style:    0x50010003
      ExStyle:    0x00000004
      Handle:    0x0000000000420A52
      I'm using the following code..
      If ControlCommand("Sharing","","[CLASS:Button; INSTANCE:1]","IsChecked","") = 0 Then
          MsgBox(0, "", "not checked")
      Else
          MsgBox(0, "", "checked")
      EndIf
      But this insists the check box isn't checked when it is.  I have also tried ControlCommand("Sharing","","[ID:1020]","IsChecked","") and ControlCommand("Sharing","","Share this folder","IsChecked","") and ControlCommand("Sharing","","&Share this folder","IsChecked",""), with no joy.
       
      Any ideas?
    • By GillesMaisonneuve
      Good morning,
       
      I am trying to align a two-line text in a button so that it would be centered in the button but can't succeed to do it for the vertical part.
      Can someone tell me what I do wrong ?
      Here is my code:
      $btn3 = GUICtrlCreateButton("Execute " & @LF & " file 1", 100, 65, 60, 50, _ BitOr($BS_CENTER,$BS_BOTTOM,$BS_MULTILINE)) and here is the resulting layout I get in my window.
      I tried to follow the help, made a lot of different parameter combinations (here it's `$BS_BOTTOM`, but I tried `$BS_VCENTER` and `$BS_TOP`), still get the same result : vertical alignement is a no go for me.
      Can someone help ?
      TIA
      Gilles

    • By Pike
      Hey Everyone, I haven't been here in a while and recently picked up my old project. Still an FNG!
      To the point: I want to be able to push the button and either play an mp3, open a file, or open an exe program (all with the same button). The problem is that I don't know how to code for that at "case 1"
              Case 1                                                                                                                                             
                  Local $sRead = IniRead("config.ini", "Config", "One", "Default")                                
                  SoundPlay($sRead, @HotKeyPressed)                                                                     <-------------------------------
                  Run($sRead, @HotKeyPressed)                                                                                 <------------------------------- This 'Run' doesn't work even by itself!
      See code below for layout. If you need more information I will attach more code or answer any questions. Any help is greatly appreciated.
       
      Much Respect,
      Pike
       
      Func _HandleButton() $iClickCount += 1 $iTimer = TimerInit() AdlibRegister("CheckButtonPress", $CheckTime) EndFunc Func CheckButtonPress() If TimerDiff($iTimer) < $CheckTime Or $iTimer = 0 Then Return Switch $iClickCount Case 1 Local $sRead = IniRead("config.ini", "Config", "One", "Default") SoundPlay($sRead, @HotKeyPressed) Run($sRead, @HotKeyPressed) Case 2 MsgBox(0, "INFORMATION:", "Choose the file you want designated to this button") Local $sFileOpenDialog = FileOpenDialog(0, @ScriptDir & "\", "All (*.*)", $FD_FILEMUSTEXIST + $FD_MULTISELECT) If @error Then MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.") EndIf Local $sFileName = InputBox("INFORMATION:", "Please Name Your File?" & @CRLF _ & @CRLF _ & "6 Characters or Less", "") IniWrite(@ScriptDir & "\config.ini", "Config", "One", $sFileOpenDialog) IniWrite(@ScriptDir & "\config.ini", "Config", "NameOne", $sFileName) EndSwitch $iClickCount = 0 AdlibUnRegister() EndFunc  

×
×
  • Create New...