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
Subz

Do you mean something like this:

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

Local $aFileRead, $aStringSplit, $aGui[0][5]
_FileReadToArray('Gui.txt', $aFileRead)
For $i = 1 To $aFileRead[0]
    $aStringSplit = StringSplit($aFileRead[$i], ',', 2)
    _ArrayTranspose($aStringSplit)
    _ArrayAdd($aGui, $aStringSplit)
Next
_ArrayDisplay($aGui)

 

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

    • guinness
      By guinness
      #include <Array.au3> #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> ; Proof of concept for using the control id as an index item for an array. I created back on 8th April 2013. Example() Func Example() ; Create the GUI. Local $iHeight = 400, $iWidth = 400 Local $hGUI = GUICreate('', $iWidth, $iHeight) GUISetState(@SW_SHOW, $hGUI) ; Declare variables to be used throughout the example. Local Const $BUTTON_ROWS_COLUMNS = 8 Local Enum $eCTRL_HWND, $eCTRL_VALUE, $eCTRL_MAX Local $aMsg[1][$eCTRL_MAX], _ $iButtonHeight = $iHeight / $BUTTON_ROWS_COLUMNS, _ $iButtonWidth = $iWidth / $BUTTON_ROWS_COLUMNS, _ $iControlID = 0 For $i = 0 To $BUTTON_ROWS_COLUMNS - 1 For $j = 0 To $BUTTON_ROWS_COLUMNS - 1 $iControlID = GUICtrlCreateButton($i & ',' & $j, $i * $iButtonWidth, $j * $iButtonHeight, $iButtonWidth, $iButtonHeight, $BS_CENTER) ; Increase the size of the array if the control id is greater than or equal to the total size of the array. If $iControlID >= UBound($aMsg) Then ReDim $aMsg[Ceiling($iControlID * 1.3)][$eCTRL_MAX] EndIf ; Add to the array. $aMsg[$iControlID][$eCTRL_HWND] = GUICtrlGetHandle($iControlID) $aMsg[$iControlID][$eCTRL_VALUE] = 'Sample string for the control id: ' & $iControlID Next Next ; Clear empty items after the last created control id. ReDim $aMsg[$iControlID + 1][$eCTRL_MAX] ; Display the array created. _ArrayDisplay($aMsg) Local $iMsg = 0 While 1 $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE ExitLoop Case $aMsg[$eCTRL_HWND][$eCTRL_HWND] To UBound($aMsg) ; If $iMsg is greater than 0 and between the 0th index of $aMsg and the last item then display in the console. If $iMsg > 0 Then ConsoleWrite('Control Hwnd: ' & $aMsg[$iMsg][$eCTRL_HWND] & ', ' & $aMsg[$iMsg][$eCTRL_VALUE] & @CRLF) EndIf EndSwitch WEnd GUIDelete($hGUI) EndFunc ;==>Example  
    • corz
      By corz
      Associative Array Functions
      I've seen a couple of UDFs for this on the forum. One of them I quite like. But it's still nearly not as good as this method, IMHO.
      I don't recall if I discovered the "Scripting.Dictionary" COM object myself or if I got the original base code from somewhere online. I have recently searched the web (and here) hard for any AutoIt references to this, other than my own over the years I've been using this (in ffe, etc..), and I can find nothing, so I dunno. If anyone does, I'd love to give credit where it's due; this is some cute stuff! It could actually be all my own work! lol
      At any rate, it's too useful to not have posted somewhere at autoitscript.com, so I've put together a wee demo.
      For those who haven't heard of the COM "Scripting.Dictionary".. 
      If you've ever coded in Perl or PHP (and many other languages), you know how useful associative arrays are. Basically, rather than having to iterate through an array to discover it's values, with an associative array you simply pluck values out by their key "names".
      I've added a few functions over the years, tweaked and tuned, and this now represent pretty much everything you need to easily work with associative arrays in AutoIt. En-joy!
      The main selling point of this approach is its simplicity and weight. I mean, look at how much code it takes to work with associative arrays! The demo is bigger than all the functions put together! The other selling point is that we are using Windows' built-in COM object functions which are at least theoretically, fast and robust.
      I've used it many times without issues, anyhow, here goes..
      ; Associative arrays in AutoIt? Hells yeah! ; Initialize your array ... global $oMyError = ObjEvent("AutoIt.Error", "AAError") ; Initialize a COM error handler ; first example, simple. global $simple AAInit($simple) AAAdd($simple, "John", "Baptist") AAAdd($simple, "Mary", "Lady Of The Night") AAAdd($simple, "Trump", "Silly Man-Child") AAList($simple) debug("It is said that Trump is a " & AAGetItem($simple, "Trump") & ".", @ScriptLineNumber);debug debug("") ; slightly more interesting.. $ini_path = "AA_Test.ini" ; Put this prefs section in your ini file.. ; [test] ; foo=foo value ; foo2=foo2 value ; bar=bar value ; bar2=bar2 value global $associative_array AAInit($associative_array) ; We are going to convert this 2D array into a cute associative array where we ; can access the values by simply using their respective key names.. $test_array = IniReadSection($ini_path, "test") for $z = 1 to 2 ; do it twice, to show that the items are *really* there! for $i = 1 to $test_array[0][0] $key_name = $test_array[$i][0] debug("Adding '" & $key_name & "'..");debug ; key already exists in "$associative_array", use the pre-determined value.. if AAExists($associative_array, $key_name) then $this_value = AAGetItem($associative_array, $key_name) debug("key_name ALREADY EXISTS! : =>" & $key_name & "<=" , @ScriptLineNumber);debug else $this_value = $test_array[$i][1] ; store left=right value pair in AA if $this_value then AAAdd($associative_array, $key_name, $this_value) endif endif next next debug(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" , @ScriptLineNumber);debug AAList($associative_array) debug(@CRLF & "Removing 'foo'..");debug AARemove($associative_array, "foo") debug(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" , @ScriptLineNumber);debug AAList($associative_array) debug(@CRLF & "Removing 'bar'..");debug AARemove($associative_array, "bar") debug(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" , @ScriptLineNumber);debug AAList($associative_array) quit() func quit() AAWipe($associative_array) AAWipe($simple) endfunc ;; Begin AA Functions func AAInit(ByRef $dict_obj) $dict_obj = ObjCreate("Scripting.Dictionary") endfunc ; Adds a key and item pair to a Dictionary object.. func AAAdd(ByRef $dict_obj, $key, $val) $dict_obj.Add($key, $val) If @error Then return SetError(1, 1, -1) endfunc ; Removes a key and item pair from a Dictionary object.. func AARemove(ByRef $dict_obj, $key) $dict_obj.Remove($key) If @error Then return SetError(1, 1, -1) endfunc ; Returns true if a specified key exists in the associative array, false if not.. func AAExists(ByRef $dict_obj, $key) return $dict_obj.Exists($key) endfunc ; Returns a value for a specified key name in the associative array.. func AAGetItem(ByRef $dict_obj, $key) return $dict_obj.Item($key) endfunc ; Returns the total number of keys in the array.. func AACount(ByRef $dict_obj) return $dict_obj.Count endfunc ; List all the "Key" > "Item" pairs in the array.. func AAList(ByRef $dict_obj) debug("AAList: =>", @ScriptLineNumber);debug local $k = $dict_obj.Keys ; Get the keys ; local $a = $dict_obj.Items ; Get the items for $i = 0 to AACount($dict_obj) -1 ; Iterate the array debug($k[$i] & " ==> " & AAGetItem($dict_obj, $k[$i])) next endfunc ; Wipe the array, obviously. func AAWipe(ByRef $dict_obj) $dict_obj.RemoveAll() endfunc ; Oh oh! func AAError() Local $err = $oMyError.number If $err = 0 Then $err = -1 SetError($err) ; to check for after this function returns endfunc ;; End AA Functions. ; debug() (trimmed-down version) ; ; provides quick debug report in your console.. func debug($d_string, $ln=false) local $pre ; For Jump-to-Line in Notepad++ if $ln then $pre = "(" & $ln & ") " & @Tab ConsoleWrite($pre & $d_string & @CRLF) endfunc  
      ;o) Cor
    • Eminence
      By Eminence
      Hello,
      Is there a way wherein I can access the data from an array coming from an Excel file then have it assigned on to a variable?
      Below is a snippet of my current code. For now, it just reads and outputs the data from the excel file and have it displayed via an array.
      #include <Array.au3> #include <Excel.au3> #include <MsgBoxConstants.au3> Local $oExcel = _Excel_Open(False) If @error Then Exit MsgBox(0, "Error", "Error creating application object." & @CRLF & "Error: " & @error & " Extends: " & @extended) ; Open Excel Woorkbook and return object Local $sWorkbook = @ScriptDir & "\Excel Files\Test Data.xlsx" Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook, False, True) If @error Then MsgBox(0, "Error", "Error opening workbook'" & $sWorkbook & ".'" & @CRLF & "Error: " & @error & "Extends: " & @extended) _Excel_Close($oExcel) Exit EndIf Local $aResult = _Excel_RangeRead($oWorkbook) ; Error Trapping If @error Then MsgBox(0, "Error", "Error reading data from '" & $sWorkbook & ".'" & @CRLF & "Error: " & @error & " Extends: " & @extended) _Excel_Close($oExcel) Exit EndIf _ArrayDisplay($aResult) My Excel file has values from Column A to H with values from 1 to 30, what I desired to do is have the value in "A7" assigned on to a variable. 
       
      Any help is appreciated. Thanks in advance.
    • Abdulla060
      By Abdulla060
      i have a 3d array that is [10][20][6] for now lets assume that its [3][3][3] so it looks something like this 
      [[[1,2,3],[1,2,3],[1,2,3]], [[1,2,3],[1,2,3],[1,2,3]], [[1,2,3],[1,2,3],[1,2,3]]] i need to add another 1d  array to the position [2][3] ( i hope its clear) so it becomes like this 
      [[[1,2,3],[1,2,3],[1,2,3]], [[1,2,3],[1,2,3],[1,2,3]], [[1,2,3],[1,2,3],[1,2,3],[4,5,6]]] and i have no idea how  
    • rudi
      By rudi
      Hello,
       
      for some script I need to investigate the states checked/unchecked, hidden/shown, enabled/disabled to several checkbox controls.
       
      Any suggestions to this script to retrieve the information in a better way?
      ; Autoit Version 3.3.14.2 ; GuiCtrlCheckState.au3 #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $CtrlW = 100 $CtrlH = 30 $GuiW = 250 $GuiH = 300 GUICreate("Test Checkbox Status", $GuiW, $GuiH) $Ctrl = GUICtrlCreateCheckbox("Checkbox", 10, 10, $CtrlW, $CtrlH) Opt("Guicoordmode", 2) $BtnDisable = GUICtrlCreateButton("Disable", -1, 10) $BtnEnable = GUICtrlCreateButton("Enable", -1, 1) $BtnHide = GUICtrlCreateButton("Hide", 10, -$CtrlH * 2 - 1) $BtnUnHide = GUICtrlCreateButton("Show (Unhide)", -1, 1) $BtnCheckChecked = GUICtrlCreateButton("IsChecked", -$CtrlW * 2 - 10, 40) $BtnCheckEnabled = GUICtrlCreateButton("IsEnabled", -1, 1) $BtnCheckHidden = GUICtrlCreateButton("IsHidden", -1, 1) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $BtnDisable GUICtrlSetState($Ctrl, $gui_disable) Case $BtnEnable GUICtrlSetState($Ctrl, $GUI_Enable) Case $BtnHide GUICtrlSetState($Ctrl, $GUI_Hide) Case $BtnUnHide GUICtrlSetState($Ctrl, $GUI_show) Case $BtnCheckChecked If CheckState($Ctrl, $GUI_CHECKED) Then ConsoleWrite("Checked" & @CRLF) Else ConsoleWrite("not checked" & @CRLF) EndIf Case $BtnCheckEnabled If CheckState($Ctrl, $GUI_Enable) Then ConsoleWrite("enabled" & @CRLF) Else ConsoleWrite("disabled" & @CRLF) EndIf Case $BtnCheckHidden If CheckState($Ctrl, $GUI_Hide) Then ConsoleWrite("Hidden" & @CRLF) Else ConsoleWrite("not hidden" & @CRLF) EndIf EndSwitch WEnd Func CheckState($_CtrlID, $_State = $GUI_CHECKED) ConsoleWrite("---------------------" & @CRLF) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $_State to check = ' & $_State & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console Local $_Read = GUICtrlRead($_CtrlID) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $_Read (GuiCtrlRead) = ' & $_Read & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console Local $_StateFound = GUICtrlGetState($_CtrlID) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $_StateFound (GuiCtrlGetState) = ' & $_StateFound & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console If $_StateFound = -1 Then MsgBox(48, "Control-ID not defined", "Control-ID invalid") Return False ElseIf BitAND($_StateFound, $_State) = $_State Then Return True ElseIf BitAND($_Read, $_State) = $_State Then Return True Else Return False EndIf EndFunc ;==>CheckState Regards, Rudi.
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.