Jump to content

Using For Loop and Array


Recommended Posts

#include <GuiConstants.au3>
Opt("TrayIconDebug", 1)

$hMain = GUICreate("MyGUI", 392, 323, -1, -1)
Global $Checkbox[3] = ["Program 1", "Program 2", "Program 3"]
$Checkbox[0] = GUICtrlCreateCheckbox($Checkbox[0], 20, 30, 100, 20)
$Checkbox[1] = GUICtrlCreateCheckbox($Checkbox[1], 20, 60, 100, 20)
$Checkbox[2] = GUICtrlCreateCheckbox($Checkbox[2], 20, 90, 100, 20)
$Run = GUICtrlCreateButton("Run", 20, 130, 50, 20)

Dim $Progress_1
Dim $Program[3]

GUISetState()

Do
    $msg = GUIGetMsg ()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Run
            $selection = False
            For $i = 0 To 2
                If ControlCommand ("MyGUI", "", $Checkbox[$i], "IsChecked") Then
                    MsgBox (0, "", $Checkbox[$i] & " is checked.")
                    $selection = True
                EndIf
            Next
                        
            If Not $selection Then
                MsgBox(16, "Error", "You must select a checkbox first...", 5)
            EndIf
                    
            If $Checkbox[0] = 1 Then function1()
            If $Checkbox[1] = 1 Then function2()
            If $Checkbox[2] = 1 Then function3()
            
            For $j = 0 To 2
                $Program[$i] = ["Program " & $i+1]
                function([$i])
            Next
    EndSelect
Until 0

Func function1()
    MsgBox(0, "", "Running program 1", 2)
    If $check2 = 1 Or $check3 = 1 Then wait_progress("Program 2")
EndFunc   ;==>function1

Func function2()
    MsgBox(0, "", "Running program 2", 2)
    If $check3 = 1 Then wait_progress("Program 3")
EndFunc   ;==>function2

Func function3()
    MsgBox(0, "", "Running program 3", 2)
    ;wait_progress("Program 3")
EndFunc   ;==>function3

Func wait_progress($sText = "next program")
    $hProg = GUICreate("Progress Bar", 361, 70, -1, -1, -1, -1, $hMain)

    $Calisto = "Calisto MT"
    $Progress_1 = GUICtrlCreateProgress(10, 40, 340, 20)
    GUICtrlSetColor(-1, 0x4169e1)
    $Label3 = GUICtrlCreateLabel("Loading " & $sText & ", please wait.", 10, 10, 300, 20)
    GUICtrlSetFont(-1, 11, 500, 0, $Calisto)

    GUISetState(@SW_SHOW, $hProg)

    For $i = 0 To 100 Step 25
        GUICtrlSetData($Progress_1, $i)
        Sleep(1000)
    Next
   
    GUIDelete($hProg)
EndFunc   ;==>wait_progressoÝ÷ ØxQÞ­íý²lª¹ë-ÈË^iÚjëh×6If $Checkbox[0] = 1 Then function1()
If $Checkbox[1] = 1 Then function2()
If $Checkbox[2] = 1 Then function3()oÝ÷ Ø׶+×~å¢m¢È¦¦XÊØ^rb¢÷×[ayÊ+­ë)¢wbîËb¢vòjezg§¶)àjëh×6For $j = 0 To 2
      $Program[$i] = ["Program " & $i+1]
      function([$i]) ---> I don't think this is right... :shifty: 
Next

However, it doesn't run correctly. Is my syntax/logic off?

HOpefully someone will understand what I'm trying to say...

Edited by dreamzboy
Link to comment
Share on other sites

Call("function" & $i+1) does exactly what you want.

Although perhaps you're better off combining those 3 functions into one, and handling $Checkbox directly inside it.

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

Hi, here's an example ...

#include <GuiConstants.au3>
Opt("TrayIconDebug", 1)

Global $Checkbox[4], $y = 30, $selection[4]
Global $spPP = StringSplit("notepad.exe|calc.exe|mspaint.exe", "|")
$hMain = GUICreate("MyGUI", 392, 323, -1, -1)
For $c = 1 To 3
    $Checkbox[$c] = GUICtrlCreateCheckbox(StringTrimRight($spPP[$c], 4), 20, $y, 100, 20)
    $y += 30
Next    
$Run = GUICtrlCreateButton("Run", 20, 130, 50, 20)
GUISetState()

While 1
    $msg = GUIGetMsg ()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Run
            function()
    EndSelect
WEnd

Func function()
    For $i = 1 To 3
        If GUICtrlRead($Checkbox[$i]) = $GUI_CHECKED Then
            MsgBox (0, "", StringTrimRight($spPP[$i], 4) & " is checked.")
            $selection[$i] = 1
        ElseIf GUICtrlRead($Checkbox[$i]) = $GUI_UNCHECKED Then
            $selection[$i] = 0
        EndIf
    Next
    If $selection[1] = 0 And $selection[2] = 0 And $selection[3] = 0 Then
        MsgBox(16, "Error", "You must select a checkbox first...", 2)
    Else
        For $g = 1 To 3
            If $selection[$g] = 1 Then ShellExecute(@SystemDir & "\" & $spPP[$g])
        Next
    EndIf 
EndFunc

Cheers

Link to comment
Share on other sites

You could simply pass all the selected checkbox numbers to the same function:

#include <GuiConstants.au3>
Opt("TrayIconDebug", 1)

$hMain = GUICreate("MyGUI", 392, 323, -1, -1)
Global $Checkbox[3] = ["Program 1", "Program 2", "Program 3"]
$Checkbox[0] = GUICtrlCreateCheckbox($Checkbox[0], 20, 30, 100, 20)
$Checkbox[1] = GUICtrlCreateCheckbox($Checkbox[1], 20, 60, 100, 20)
$Checkbox[2] = GUICtrlCreateCheckbox($Checkbox[2], 20, 90, 100, 20)
$Run = GUICtrlCreateButton("Run", 20, 130, 50, 20)

Dim $Progress_1
Dim $Program[3]

GUISetState()

Do
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Run
            $selection = False
            For $i = 0 To 2
                If ControlCommand("MyGUI", "", $Checkbox[$i], "IsChecked") Then
                    _CheckBoxFunction($i)
                    $selection = True
                EndIf
            Next

            If Not $selection Then
                MsgBox(16, "Error", "You must select a checkbox first...", 5)
            EndIf
    EndSelect
Until 0

Func _CheckBoxFunction($iSelect)
    Switch $iSelect
        Case 0
            MsgBox(0, "", "Running program 1", 2)
            wait_progress("Program 1")
        Case 1
            MsgBox(0, "", "Running program 2", 2)
            wait_progress("Program 2")
        Case 2
            MsgBox(0, "", "Running program 3", 2)
            wait_progress("Program 3")
    EndSwitch
EndFunc   ;==>_CheckBoxFunction

Func wait_progress($sText = "next program")
    $hProg = GUICreate("Progress Bar", 361, 70, -1, -1, -1, -1, $hMain)

    $Calisto = "Calisto MT"
    $Progress_1 = GUICtrlCreateProgress(10, 40, 340, 20)
    GUICtrlSetColor(-1, 0x4169e1)
    $Label3 = GUICtrlCreateLabel("Loading " & $sText & ", please wait.", 10, 10, 300, 20)
    GUICtrlSetFont(-1, 11, 500, 0, $Calisto)

    GUISetState(@SW_SHOW, $hProg)

    For $i = 0 To 100 Step 25
        GUICtrlSetData($Progress_1, $i)
        Sleep(1000)
    Next

    GUIDelete($hProg)
EndFunc   ;==>wait_progress

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...