Jump to content

Checkbox, from array, from text file...


StMaSi
 Share

Recommended Posts

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.

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 years later...

Thought I'd post answers to your pm here:

Create an in for example Installations.ini

[Installations]
Adobe Acrobat = Msiexec.exe /i <Path to Msi> /QN /NoRestart
Microsoft Office = Setup.exe /adminfile MSOfffice.msp

Use something like the following to allow users to create custom batch script:

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Global $g_sInstalls = @ScriptDir & "\Installs.cmd"
Global $g_aInstallations = IniReadSection(@ScriptDir & "\Installations.ini", "Installations")
    If @error Then Exit MsgBox(4096, "Error", "Error Reading [Installations] Section")
    ;~ Lets add another column for Checkbox id
    _ArrayColInsert($g_aInstallations, 0)

Example()

Func Example()
    Local $iTop = 10, $iBottom = 10, $iY = 10, $iHeight = 25
    Local $hGUI = GUICreate("Installations", 300, $iTop + ($g_aInstallations[0][1] * $iHeight) + $iHeight + $iBottom)
    For $i = 1 To $g_aInstallations[0][1]
        $g_aInstallations[$i][0] = GUICtrlCreateCheckbox($g_aInstallations[$i][1], 10, $iY, 185, $iHeight)
        $iY += 25
    Next
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, $iY, 85, $iHeight)
    GUISetState()
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop
            Case $g_aInstallations[1][0] To $g_aInstallations[$g_aInstallations[0][1]][0]
                $hInstalls = FileOpen($g_sInstalls, 10)
                For $i = 1 To $g_aInstallations[0][1]
                    If _IsChecked($g_aInstallations[$i][0]) Then
                        FileWrite($hInstalls, $g_aInstallations[$i][2] & @CRLF)
                    EndIf
                Next
                FileClose($hInstalls)
        EndSwitch
    WEnd
EndFunc

Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

 

Link to comment
Share on other sites

@DonJK Check that when you copied from the forum that it didn't include any hidden characters (use notepad++)

Make sure that the file "Installations.ini" is saved into the same directory as the script, please post results from the Scite output console if you're still getting errors and please use the forum rather than pm.

Link to comment
Share on other sites

  • 1 month later...
  • 4 weeks later...

If you had a combo box it would be filled with states and the when you select a state it would reveal the attractions associated with that state. For the New York dropdown the checkboxes would be the Statue if Liberty, Time Square, and the Brooklyn Bridge. California would be Disneyland, Golden gate Bridge and Alcatraz.

Link to comment
Share on other sites

Here to get you started :

#include <GUIConstants.au3>

Local $aStates[] = ["New York", "California"]
Local $aAttractions[][] = [["Statue of Liberty", "Time Square", "Brooklyn Bridge"], ["Disneyland", "Golden gate Bridge", "Alcatraz"]]

GUICreate('List of attractions', 250, 250)
Local $idCombo = GUICtrlCreateCombo("", 10, 30, 80, 28)
For $i = 0 To UBound($aStates) - 1
  GUICtrlSetData($idCombo, $aStates[$i])
Next
Local $aCB[UBound($aAttractions, 2)]
For $i = 0 To UBound($aAttractions, 2) - 1
  $aCB[$i] = GUICtrlCreateCheckbox("", 110, 10 + $i * 30, 100, 25)
  GUICtrlSetState(-1, $GUI_HIDE)
Next

GUISetState()
Local $iState
While 1
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
    Case $idCombo
      $iState = CheckState(GUICtrlRead($idCombo))
      For $i = 0 To UBound($aAttractions, 2) - 1
        GUICtrlSetData($aCB[$i], $aAttractions[$iState][$i])
        GUICtrlSetState($aCB[$i], $GUI_SHOW + $GUI_UNCHECKED)
      Next
  EndSwitch
WEnd

Func CheckState($sState)
  For $i = 0 To UBound($aStates) - 1
    If $aStates[$i] = $sState Then Return $i
  Next
EndFunc   ;==>CheckState

 

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