Jump to content
StMaSi

Checkbox, from array, from text file...

Recommended Posts

StMaSi

So, I have a text file like this...

"Label", {left}, {top}, {width}, {height}
"Label", {left}, {top}, {width}, {height}
"Label", {left}, {top}, {width}, {height}
"Label", {left}, {top}, {width}, {height}
"Label", {left}, {top}, {width}, {height}

What I'm attempting to do is read the data from this file, into an array, and use it to create checkboxes on a form like this...

For $cb = 0 to 4     ; line numbers
    $Checkbox[$cb] = GUICtrlCreateCheckbox({label}, {left}, {top}, {width}, {height})
Next

However, I can't figure out how to read the file into the array so as to be able to assign the data elements appropriately. I'm obviously missing something (or more than likely more than one something), but can't figure it out.

Can anyone assist with this?

Thanx.

 

Share this post


Link to post
Share on other sites
StMaSi

Yes, that creates the array perfectly. Now, if I were to use the GUICtrlCreateCheckBox() within that piece of code, I think I would insert it right before the next, correct? Or, do I now need another looper to read the array and create the checkboxes? Thanx again!

Share this post


Link to post
Share on other sites
Subz

Technically you don't need the array, you can just use:

#Include <Array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>

Local $aFileRead, $aStringSplit
GUICreate('', 200, 110)
_FileReadToArray('Gui.txt', $aFileRead)
For $i = 1 To $aFileRead[0]
    $aStringSplit = StringSplit($aFileRead[$i], ',', 2)
    If UBound($aStringSplit) -1  = 4 Then
        GUICtrlCreateCheckbox($aStringSplit[0], Number($aStringSplit[1]), Number($aStringSplit[2]), Number($aStringSplit[3]), Number($aStringSplit[4]))
    EndIf
Next
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Gui.txt

"Label1", 5, 5, 190, 20
"Label2", 5, 25, 190, 20
"Label3", 5, 45, 190, 20
"Label4", 5, 65, 190, 20
"Label5", 5, 85, 190, 20

 

Share this post


Link to post
Share on other sites
StMaSi

Excellent! Just what the Dr. ordered. Thank you so much!!!

Share this post


Link to post
Share on other sites
StMaSi

Dang. With the checkboxes being created in this manner, how on earth would I be able to tell which ones are checked?

The goal is to have the user select one or more options, then the script would install their choices like this...

29j3hl.jpg

Would I have to create a label/tag for each checkbox during their creation, or something along those lines, so as to be able to target them afterward?

Thanx.

Share this post


Link to post
Share on other sites
Subz

You could do it a few ways for example:

1. add another column with an Array Name for example: Adobe_Flash_Player, label, x, y, w, h, then you can use the Assign function.
2. Use the original array to assign the variable for example: $FileRead[$i] = GUICtrlCreateCheckbox($aStringSplit[0], Number($aStringSplit[1]), Number($aStringSplit[2]), Number($aStringSplit[3]), Number($aStringSplit[4]))

Either way you can then use these to capture there states when you click Install.

Edited by Subz

Share this post


Link to post
Share on other sites
StMaSi

So, I can't seem to get the Assign function to work. Is it because I'm attempting to utilize one of the array elements? Thanx.

Share this post


Link to post
Share on other sites
Subz

Not sure it works for me:

#Include <Array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>

Local $aFileRead, $aStringSplit
GUICreate('', 205, 135)
_FileReadToArray('Gui.txt', $aFileRead)
Local $aFileItem[$aFileRead[0]]
For $i = 1 To $aFileRead[0]
    $aStringSplit = StringSplit($aFileRead[$i], ',', 2)
    If UBound($aStringSplit) -1  = 5 Then
        Assign($aStringSplit[0], GUICtrlCreateCheckbox($aStringSplit[1], Number($aStringSplit[2]), Number($aStringSplit[3]), Number($aStringSplit[4]), Number($aStringSplit[5])))
        $aFileItem[$i - 1] = $aStringSplit[0]
    EndIf
Next
$hCancel = GUICtrlCreateButton('Cancel', 5, 105, 95, 25)
$hInstall = GUICtrlCreateButton('Install', 105, 105, 95, 25)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $hCancel
            ExitLoop
        Case $hInstall
            For $i = 0 To UBound($aFileItem) - 1
                If GUICtrlRead(Eval($aFileItem[$i])) = $GUI_CHECKED Then
                    ConsoleWrite('IsChecked : ' & $aFileItem[$i] & @CRLF)
                EndIf
            Next
    EndSwitch
WEnd

Ini File

Var1, Label 1, 5, 5, 190, 20
Var2, Label 2, 5, 25, 190, 20
Var3, Label 3, 5, 45, 190, 20
Var4, Label 4, 5, 65, 190, 20
Var5, Label 5, 5, 85, 190, 20

 

Share this post


Link to post
Share on other sites
StMaSi

I was close to your suggestion here, but no cigar. I modified my code to reflect your code, but swapped a MsgBox for your ConsoleWrite and now I can see the "Var#" label in the MsgBox. However, if the "Var#" value contains a period, then nothing occurs when I click the Install button. Is there a reason a period can't be within the variable value? Thanx again.

Share this post


Link to post
Share on other sites
Malkey

This modified Subz's example automates the checkbox positions on the GUI, so that the positions are not required to be stored in the ini file.

#include <GUIConstantsEx.au3>

Local $aFileRead = ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5"]
; Or
;Local $aFileRead = FileReadToArray('Gui.txt')

Local $aID_CkBx[UBound($aFileRead)], $sResults

GUICreate('', 205, 135)

For $i = 0 To UBound($aID_CkBx) - 1
    $aID_CkBx[$i] = GUICtrlCreateCheckbox($aFileRead[$i], 5, (20 * $i) + 5, 190, 20) ; 1 column, many rows.
    ; Or
    ;$aID_CkBx[$i] = GUICtrlCreateCheckbox($aFileRead[$i], 5 + (Int($i / 3) * 95), ((20 * Mod($i, 3)) + 5), 95, 20) ; 3 rows, many columns.
Next

Local $hCancel = GUICtrlCreateButton('Cancel', 5, 105, 95, 25)
Local $hInstall = GUICtrlCreateButton('Install', 105, 105, 95, 25)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $hCancel
            ExitLoop
        Case $hInstall
            For $i = 0 To UBound($aID_CkBx) - 1
                If GUICtrlRead($aID_CkBx[$i]) = $GUI_CHECKED Then
                    $sResults &= $aFileRead[$i] & ' is checked.' & @CRLF
                EndIf
            Next
            MsgBox(0, "Results", $sResults)
            $sResults = ""
    EndSwitch
WEnd

'Gui.txt' -  The ini file used, if needed.  (The labels could be store in an array in the script.)

Label 1
Label 2
Label 3
Label 4
Label 5

You don't have to use this method,  it's just an alternative that could come in handy one day.

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

  • Similar Content

    • lattey
      By lattey
      hi,
      i have checkboxes and each checkbox that checked, i put in array. 
      now, im stuck on how to loop the checked array and store in in one variable. what i can do now, is only write the result into a text file. 
      below is the code:
      #include <GUIConstantsEx.au3> ;~ #include <MsgBoxConstants.au3> #include <ButtonConstants.au3> #include <Array.au3> Global $Count = 3 Global $CheckBoxP[$Count] Global $step[$Count] global $array1[1] Global $ExitResult $hGUI = GUICreate("Summary Steps", 500, 400) GUISetFont(12, 400, "Tahoma") GUICtrlCreateLabel( "Please Select the Summary Steps for Script Check", 70, 20) GUISetFont(10, 400, "Tahoma") Global $array_Pstep[3] = ["fix2","fix1","fix3"] global $step[3] = ["2","3","4"] $Spacing = 50 For $i = 0 To UBound($array_Pstep) - 1 $CheckBoxP[$i] = GUICtrlCreateCheckbox($array_Pstep[$i], 80, $Spacing + (20 * $i), 65, 17) Next $submit = GUICtrlCreateButton("Submit",180, 280, 80, 30) $exit = GUICtrlCreateButton("Exit",180, 320, 80, 30) GUISetState() While 1 $Msg = GUIGetMsg() Select case $Msg=$submit For $i = 0 To $Count - 1 If GUICtrlRead($CheckBoxP[$i]) = $GUI_CHECKED Then _ArrayAdd($array1, $step[$i]) EndIf Next Global $logfilerray = @WorkingDir & "\checkedlist.txt" FileDelete ($logfilerray) Global $readlogfile = FileOpen($logfilerray,1) for $a = 1 to UBound($array1) - 1 ;~ $var=$array1[$a] FileWriteLine($readlogfile,$array1[$a]) Next FileClose($readlogfile) Exit case $Msg=$exit $ExitResult = MsgBox(1,"Summary Step", "Continue to Exit ?") if $ExitResult = 1 Then ;ok Exit EndIf Exit EndSelect WEnd  
    • omicron
      By omicron
      How do you perform a nested loop function with a multidimensional array from 2 lists.
      for i in list1
      (open file) extract variable
          while open for i in list 2
          (open file2) extract variable
       
      var1 + var2 = (search term)

      The list sizes will more than likely consist of different lengths.
       
      What is the best approach to accomplishing this method?
             
    • omicron
      By omicron
      Hello!

      I am working on a function that I am just getting lost on. The goal is a multiple nested loop.

      Here are the steps:
      Contents of file1.txt::
      [topic] var1=Name var2=OtherName var3=SomeotheName Contents of file2.txt::
      [subTopic] top=sub1 top2=sub2 top3=sub3 The Shell I am working from::
      #include <file.au3> $file = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) msgbox(0,'','the line ' & $i & ' is ' & $line) Next FileClose($file) Understanding however that the "msgbox" needs to then become a variable. in example the following::
      $file = "c:\yourfile.txt" FileOpen($file, 0) While true( prog.exe is running && "WinName" is open) do For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ;Open File to log "current location of file 1" FileWriteLine ("filename", $i & ' is ' & $line) var = $line Next $file2 = "c:\yourfile.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) ; OpenFile to log "Current location of file 2" FileWriteLine ("filename", $i & ' is ' & $line) Next FileClose($file2) FileClose($file) The goal in written form is the following ::

      While in "OpenWindow"
          read from file 1 starting at line 1 until end of file.
         file 1 is a list of names to be searched.
         With $line selected, add this element to the element in file 2.
       
      The search of a variables in list 1 and list 2 differ on the amount of posts that day. (This is not a web based platform, it is a game) I need to search 2 names and take a screenshot of the out put. The sizes of the names list depend on the activity of names at the time of search.
      This loop continues until all the names from both lists have been searched. Mostly in the format of::
      File1= item
      File2= Vendor
       
      Item + Vendor  ( Capture screen, scroll) -- Not sure how to detect if I need to scroll)
       
      Thank you for your help and support!
    • stick3r
      By stick3r
      Hi, I have this script and I need to reset all checkboxes to UNCHECKED when button is pressed.
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= Global $Form = GUICreate("Checklist", 190, 407, 1233, 178) Global $text = "" Global $CheckBox GUICtrlCreateTab(1, 1, 190, 365) GUICtrlCreateTabItem("TAB 1") Global $InputCaseID1 = GUICtrlCreateInput($text, 80, 25, 80, 21) Global $lblCaseID = GUICtrlCreateLabel("Case ID", 8, 30, 68, 18) Global $CheckBox1 = GUICtrlCreateCheckbox("1111111", 8, 48, 110, 17) Global $CheckBox2 = GUICtrlCreateCheckbox("2222222", 8, 72, 110, 17) Global $CheckBox3 = GUICtrlCreateCheckbox("333333333", 8, 96, 110, 17) Global $CheckBox4 = GUICtrlCreateCheckbox("444444444", 8, 120, 110, 17) Global $CheckBox5 = GUICtrlCreateCheckbox("55555555", 8, 144, 110, 17) Global $CheckBox6 = GUICtrlCreateCheckbox("666666666", 8, 168, 110, 17) Global $CheckBox7 = GUICtrlCreateCheckbox("77777777", 8, 192, 150, 17) Global $CheckBox8 = GUICtrlCreateCheckbox("888888888", 8, 216, 150, 17) Global $CheckBox9 = GUICtrlCreateCheckbox("99999999", 8, 240, 110, 17) Global $CheckBox10 = GUICtrlCreateCheckbox("45646", 8, 264, 110, 17) Global $CheckBox11 = GUICtrlCreateCheckbox("4345634", 8, 288, 97, 17) Global $CheckBox12 = GUICtrlCreateCheckbox("4563456", 8, 312, 97, 17) Global $CheckBox13 = GUICtrlCreateCheckbox("456456", 8, 336, 97, 17) GUICtrlCreateTabItem("TAB 2") Global $InputCaseID2 = GUICtrlCreateInput("", 80, 25, 80, 21) Global $lblCaseID = GUICtrlCreateLabel("Case ID", 8, 30, 68, 18) Global $CheckBox14 = GUICtrlCreateCheckbox("AAAAAAA", 8, 48, 110, 17) Global $CheckBox15 = GUICtrlCreateCheckbox("PPPPPPPPP", 8, 72, 110, 17) Global $CheckBox16 = GUICtrlCreateCheckbox("BBBBBBBB", 8, 96, 110, 17) Global $CheckBox17 = GUICtrlCreateCheckbox("CCCCCCCCCCCCC", 8, 120, 110, 17) Global $CheckBox18 = GUICtrlCreateCheckbox("DDDDDDDDDDDDD", 8, 144, 110, 17) Global $CheckBox19 = GUICtrlCreateCheckbox("EEEEEEEEEE", 8, 168, 110, 17) Global $CheckBox20 = GUICtrlCreateCheckbox("FFFFFFFFFFF", 8, 192, 150, 17) Global $CheckBox21 = GUICtrlCreateCheckbox("GGGGGGGGGG", 8, 216, 150, 17) Global $CheckBox22 = GUICtrlCreateCheckbox("HHHHHHHHH", 8, 240, 110, 17) Global $CheckBox23 = GUICtrlCreateCheckbox("IIIIIIIII", 8, 264, 110, 17) Global $CheckBox24 = GUICtrlCreateCheckbox("JJJJJJJ", 8, 288, 97, 17) Global $CheckBox25 = GUICtrlCreateCheckbox("KKKKKKKKKKK", 8, 312, 97, 17) Global $CheckBox26 = GUICtrlCreateCheckbox("LLLLLLLLL", 8, 336, 97, 17) GUICtrlCreateTabItem("") Global $CheckBoxAlwaysOnTop = GUICtrlCreateCheckbox("Always on Top", 8, 380, 97, 17) Global $btnReset = GUICtrlCreateButton("RESET", 104, 365, 75, 41) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $CheckBoxAlwaysOnTop _SetAlwaysOnTop() Case $btnReset For $i = 7 To 19 Step 1 ;Remove checked CheckBoxes GUICtrlSetState($CheckBox&$i, $GUI_UNCHECKED) Next For $i = 23 To 35 Step 1 GUICtrlSetState($CheckBox&$i, $GUI_UNCHECKED) Next GUICtrlSetData($InputCaseID1, "") GUICtrlSetData($InputCaseID2, "") EndSwitch WEnd Func _SetAlwaysOnTop() If GUICtrlRead($CheckBoxAlwaysOnTop) = $GUI_CHECKED Then WinSetOnTop($Form, "", 1) ;On top Else WinSetOnTop($Form, "", 0) ;Not on top EndIf EndFunc ;==>_SetAlwaysOnTop My question is:
      Why this  For loop for GUICtrlSetState($CheckBox&$i, $GUI_UNCHECKED) does not work properly
      I have $CheckBox1, $CheckBox2.......till $CheckBox26 and with For $i = 1 To 26 it does not work. It only unchecks few of them, but not all.
      Later I have discovered that For $i = 7 To 19 removes  $CheckBox1 to  $CheckBox13 and For $i = 23 To 35 removes  $CheckBox14 to  $CheckBox26
      Any idea why?
    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
×