txt auslesen und für jede Zeile Button + Checkbox erstellen

  • Hi,

    ich bin noch sehr neu bei AutoIT und möchte für mich ein kleines Programm haben das aus einer *.txt Zeile für Zeile ausliest und aus jeder Zeile automatisch einen Button + Checkbox erstellt

    So schaut die txt aus:
    PC1
    PC2
    PC3

    Über die
    Checkbox kann ich dann am Ende einen, mehrere oder alle PC´s auswählen die dann ein Prog installieren wenn ich auf Install klicke.

    Leider bekomme ich als Anfänger nur die Buttons mit Programmaufruf hin, aber schaffe es nicht die einzelnen Zeilen der txt in einen Button und eine Checkbox um zu wandeln.

    Habe euch mal ein Bsp gemacht wie es später ungefähr aus sehen soll.

    [autoit]

    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_Outfile=PC.exe
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    #include <ButtonConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #Region ### START Koda GUI section ### Form=c:\pc\pc.kxf
    $Form1 = GUICreate("PC Update Tool", 609, 430, 192, 125)
    $Ueberschrift = GUICtrlCreateLabel("Wähle einen oder alle Clients aus", 13, 10, 390, 40)
    GUICtrlSetFont(-1, 18, 400, 0, "Comic Sans MS")
    $PC1 = GUICtrlCreateButton("PC1", 20, 180, 200, 40)
    GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
    $PC2 = GUICtrlCreateButton("PC2", 20, 240, 200, 40)
    GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
    $PC3 = GUICtrlCreateButton("PC3", 20, 304, 200, 40)
    GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
    $Alle = GUICtrlCreateButton("Alle PC´s", 20, 64, 200, 40)
    GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
    $Auswahl1 = GUICtrlCreateCheckbox("Auswahl PC1", 248, 184, 97, 25)
    $Auswahl2 = GUICtrlCreateCheckbox("Auswahl PC2", 248, 240, 97, 25)
    $Auswahl3 = GUICtrlCreateCheckbox("Auswahl PC3", 248, 312, 97, 25)
    $AuswahlAlle = GUICtrlCreateCheckbox("Auswahl Alle PC´s", 248, 64, 129, 25)
    $Install = GUICtrlCreateButton("Install", 440, 168, 113, 57)
    GUICtrlSetFont(-1, 20, 400, 0, "Comic Sans MS")
    GUICtrlSetColor(-1, 0xFF0000)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit][autoit][/autoit][autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $PC1
    ShellExecuteWait("test.exe" , "" , "C:\Test" , "" , @SW_HIDE)
    Case $PC2
    MsgBox(262208,"Button-Information","Button für PC2 wurde gedrückt")
    Case $PC3
    MsgBox(262208,"Button-Information","Button für PC3 wurde gedrückt")
    EndSwitch
    WEnd

    [/autoit]


    Aus einer txt auslesen und eine msgbox erstellen klappt

    [autoit]


    $datei = FileRead("pc.txt")

    [/autoit][autoit][/autoit][autoit]

    $variable = StringSplit($datei,
    @CRLF)

    [/autoit][autoit][/autoit][autoit]

    For $i = 1 To $variable[0] Step 2

    [/autoit][autoit][/autoit][autoit]

    MsgBox(0, "", $variable[$i])

    [/autoit][autoit][/autoit][autoit]

    Next

    [/autoit]


    nur die Buttons + Checkboxen bekomme ich nicht hin. Event hat wer einen kleinen Tipp für mich

    Besten Dank

  • Hier mal ein kleiner Ansatz:

    [autoit]

    $f = StringSplit(FileRead('pc.txt'), @CRLF)
    $idButtons[$f[0]]

    [/autoit][autoit][/autoit][autoit]

    For $i = 1 To $f[0]
    $idButtons[$i-1] = GUICtrlCreateButton($f[$i], 20, 180, 200, 40)
    GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")
    Next

    [/autoit]


    Dann enhaelt $idButtons[0] das Handle des "PC1" - Button, $idButtons[1] das Handle des "PC2" - Button usw.

    There's a joke that C has the speed and efficieny of assembly language combined with readability of....assembly language. In other words, it's just a glorified assembly language. - Teh Interwebz

    C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, you blow off your whole leg. - Bjarne Stroustrup
    Genie zu sein, bedeutet für mich, alles zu tun, was ich will. - Klaus Kinski

  • Dann enhaelt $idButtons[0] das Handle des "PC1" - Button, $idButtons[1] das Handle des "PC2" - Button usw.

    Bringt dir nur nix, weil bei dir alle Buttons an der selben Position sind und sich überlappen. Wenn man das dynamisch erstellt muss man natürlich auch noch die Position in Abhängigkeit des Schleifenzählers berechnen.

    Also sowas in der Art:

    [autoit]


    $idButtons[$i-1] = GUICtrlCreateButton($f[$i], 20, 120 + ($i*60), 200, 40)

    [/autoit]
  • Bringt dir nur nix, weil bei dir alle Buttons an der selben Position sind und sich überlappen. Wenn man das dynamisch erstellt muss man natürlich auch noch die Position in Abhängigkeit des Schleifenzählers berechnen.

    Also sowas in der Art:

    [autoit]


    $idButtons[$i-1] = GUICtrlCreateButton($f[$i], 20, 120 + ($i*60), 200, 40)

    [/autoit]


    Ja natuerlich, wie bloed von mir. Ich hatte das mal schnell im Browser getippt.
    Aber mal abgesehen davon, ich sagte ja auch 'Ansatz' und nicht 'fertiges Skript' :P

    There's a joke that C has the speed and efficieny of assembly language combined with readability of....assembly language. In other words, it's just a glorified assembly language. - Teh Interwebz

    C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, you blow off your whole leg. - Bjarne Stroustrup
    Genie zu sein, bedeutet für mich, alles zu tun, was ich will. - Klaus Kinski

    • Offizieller Beitrag

    Wenn du im Programmverlauf eine GUI dynamisch erstellst, kannst du so vorgehen (Bsp. mit Inputs)

    Spoiler anzeigen
    [autoit]

    Global $GUI, $aInput[2] = [1], $btAdd

    [/autoit] [autoit][/autoit] [autoit]

    $GUI = GUICreate('Test', 400, 50)
    $aInput[1] = GUICtrlCreateInput('', 10, 10, 300, 21)
    GUICtrlSetResizing(-1, 802) ; $GUI_DOCKALL
    $btAdd = GUICtrlCreateButton('Add Input', 320, 10, 70, 21)
    GUICtrlSetResizing(-1, 802) ; $GUI_DOCKALL
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    Switch GUIGetMsg()
    Case -3
    Exit
    Case $btAdd
    _NewCtrl()
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _NewCtrl()
    Local $iDiff = 31 ; == Abstand + Höhe ==> 10 + 21
    Local Static $iY = 10 ; == Startwert vom ersten Ctrl
    $iY += $iDiff
    $aInput[0] += 1
    ReDim $aInput[$aInput[0]+1]
    Local $aWin = WinGetPos($GUI)
    WinMove($GUI, '', $aWin[0], $aWin[1], $aWin[2], $aWin[3] + 31)
    $aInput[$aInput[0]] = GUICtrlCreateInput('', 10, $iY, 300, 21)
    GUICtrlSetResizing(-1, 802) ; $GUI_DOCKALL
    EndFunc

    [/autoit]
  • Glaub Listview wäre da praktischer…

    [Blockierte Grafik: http://stefan.blagojevic.at/loading.gif]

    Design, Modellbau, CAD <3
    AutoCAD, ArchiCAD, REVIT (ist ein Scheiss, habe aber das Zertifikat)

    Cinema 4D, RuckZuck Statik Programm

    Michael Bay als Architekt


    Da eine Glasfassade! Booom Sichtbeton! Laminiertes Bild auf Mosaiksteinchen! Granit! Granit! Granit! Sichtbetonwand mit 50° Neigung!
    Holzverkleidung erscheint da! Boooooom!

  • Erst einmal vielen Dank für die Ansätze. Da ich noch frisch in AutoIT bin habe ich mit nochmals Gedanken gemacht und das Script weitergeschrieben und bin dabei auf "neue" Probleme gestoßen.

    Ich möchte ja nur das für jede Zeile ein Button und eine Checkbox erstellt werden, bei mir im Script wird aber eine feste Anzahl vergeben, was das ganze sehr unflexibel macht und ich eigentlich keine Platzhalter möchte, sondern die genaue Anzahl der PC´s.

    Außerdem wollte ich wenn ich die Checkbox $cball auswähle ein Haken bei allen anderen Checkboxen gesetzt wird.

    Wenn ich den Install Button drücke soll bei allen ausgewählten Checkboxen ein Programm installiert werden.

    Die data.txt liegt direkt im Scriptdir und schaut wie folgt aus:
    PC1
    PC2
    PC3

    Brauche für die offenen Fragen nochmals eine Denkansatz bitte.

    Anbei noch das Script:

    [autoit]


    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_Outfile=Test4.exe
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #include <ButtonConstants.au3>
    #Include <File.au3>

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    $datei = FileOpen(@ScriptDir & "\data.txt")
    $host1 = FileReadLine($datei,1)
    $host2 = FileReadLine($datei,2)
    $host3 = FileReadLine($datei,3)
    $host4 = FileReadLine($datei,4)
    FileClose(@ScriptDir & "\data.txt")

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    #Region ### START Koda GUI section ###
    $Form1 = GUICreate("PC Update Tool", 683, 995, 199, 104)
    $Ueberschrift = GUICtrlCreateLabel("Wähle einen, mehrere oder alle Clients aus", 61, 10, 566, 40, $SS_CENTER)
    GUICtrlSetFont(-1, 18, 400, 0, "Comic Sans MS")

    [/autoit][autoit][/autoit][autoit]

    $Alle = GUICtrlCreateButton("Alle Clients", 20, 96, 168, 40)
    GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")

    [/autoit][autoit][/autoit][autoit]

    $Install = GUICtrlCreateButton("Install", 480, 88, 113, 57)
    GUICtrlSetFont(-1, 20, 400, 0, "Comic Sans MS")
    GUICtrlSetColor(-1, 0xFF0000)

    [/autoit][autoit][/autoit][autoit]

    $b1 = GUICtrlCreateButton($host1, 20, 210, 168, 40)
    GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")

    [/autoit][autoit][/autoit][autoit]

    $b2 = GUICtrlCreateButton($host2, 340, 210, 168, 40)
    GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")

    [/autoit][autoit][/autoit][autoit]

    $b3 = GUICtrlCreateButton($host3, 20, 275, 168, 40)
    GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")

    [/autoit][autoit][/autoit][autoit]

    $b4 = GUICtrlCreateButton($host4, 340, 275, 168, 40)
    GUICtrlSetFont(-1, 14, 400, 0, "Comic Sans MS")

    [/autoit][autoit][/autoit][autoit]

    $cball = GUICtrlCreateCheckbox("Auswahl Alle Clients", 216, 104, 129, 25)
    $cb1 = GUICtrlCreateCheckbox($host1, 216, 215, 97, 25)
    $cb2 = GUICtrlCreateCheckbox($host2, 544, 215, 97, 25)
    $cb3 = GUICtrlCreateCheckbox($host3, 216, 280, 97, 25)
    $cb4 = GUICtrlCreateCheckbox($host4, 544, 280, 97, 25)
    GUISetState(@SW_SHOWNORMAL)
    #EndRegion ### END Koda GUI section ###

    [/autoit][autoit][/autoit][autoit][/autoit][autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $b1
    ShellExecuteWait("test.exe" , "" , "C:\Test" , "" , @SW_HIDE)
    Case $b2
    MsgBox(262208,"Button-Information","Button für PC2 wurde gedrückt")
    Case $b3
    MsgBox(262208,"Button-Information","Button für PC3 wurde gedrückt")
    Case $Install

    [/autoit][autoit][/autoit][autoit]

    If GUICtrlRead($cb1) = $GUI_CHECKED Then
    MsgBox(0,"","Checkbox aktiv")
    Else
    MsgBox(0,"","Checkbox inaktiv")
    EndIf
    EndSwitch
    WEnd

    [/autoit]
    • Offizieller Beitrag

    Ich habe mein voriges Bsp. mal umgemodelt auf Checkboxerstellung. Das ist dynamisch und erstellt nur die Anzahl Checkboxen, die du benötigst.
    Ich habe die Erstellung der Checkbox zur An/Abwahl aller Checkboxen an die erste Position der Textdatei gesetzt, kannst du natürlich ändern.

    Spoiler anzeigen
    [autoit]

    Global $GUI
    Global $aFile, $FilePath = "Deine_Text_Datei" ; Pfad anpassen
    _FileReadToArray($FilePath, $aFile)

    [/autoit] [autoit][/autoit] [autoit]

    ; == hier simuliere ich das eingelesene Array - nicht verwenden, wenn aus Datei gelesen wird
    Global $aFile[5] = [4,'Alle auswählen','PC-1','PC-2','PC-3']
    ; == Ende Simulation

    [/autoit] [autoit][/autoit] [autoit]

    Global $aCheckBox[$aFile[0]], $x = 10, $yStart = -20, $diff = 30

    [/autoit] [autoit][/autoit] [autoit]

    $GUI = GUICreate('Test', 400, 20)
    For $i = 1 To $aFile[0]
    $aWin = WinGetPos($GUI)
    WinMove($GUI, '', $aWin[0], $aWin[1], $aWin[2], $aWin[3] + $diff)
    $y = $yStart+$i*$diff
    $aCheckBox[$i-1] = GUICtrlCreateCheckbox($aFile[$i], $x, $y, 100, 20)
    GUICtrlSetResizing(-1, 802) ; $GUI_DOCKALL
    Next
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case -3
    Exit
    EndSwitch
    $iAll = 0
    $iState = $GUI_CHECKED
    For $i = 0 To UBound($aCheckBox) -1
    If $nMsg = $aCheckBox[$i] Then
    If $i = 0 Then
    $iAll = 1
    If Not BitAND(GUICtrlRead($aCheckBox[$i]), $iState) Then $iState = $GUI_UNCHECKED
    EndIf
    EndIf
    If $iAll = 1 Then GUICtrlSetState($aCheckBox[$i], $iState)
    Next
    WEnd

    [/autoit]