Sign in to follow this  
Followers 0
xzaz

Create cases from Array

17 posts in this topic

Yep got another problem :)

I made this script so far:

#Include<GuiConstants.au3>
#include <Array.au3>
$path = "programmas/progs.ini"
$rsn = IniReadSectionNames($path)
$y = 0

$Form2 = GUICreate("", 368, 600, 321, 254)
 For $i = 1 To $rsn[0]
     
        $rs = IniReadSection($path,$rsn[$i])
        
        if $rs[4][1] = "Ja" Then 
            $buttonpl = $buttonpl + 100 ;==> Begin hoogte
        Else
            $buttonpl = $buttonpl +30 ;==> Hoeveel pix zit er tussen de knoppen
        Endif
        
        GuiCtrlCreateLabel("", 10, $buttonpl, 30, 30,$BS_DEFPUSHBUTTON + $BS_FLAT)
        GUICtrlSetBkColor(-1,$rs[3][1])
        $rs[5][1] = GuictrlCreateButton(" "&$rs[1][1],40,$buttonpl,200,30,$BS_DEFPUSHBUTTON + $BS_FLAT + $BS_LEFT)
                GUICtrlSetFont (-1,10, 400, 1, "Verdana")

Next

GUISetState(@SW_SHOW)
Do 
 Sleep(10)
        
    $msg = GUIGetMsg()
    Switch $msg
        
         For $i = 1 To $rsn[0]
            Case $rs[5][1] 
            msgBox(0,"","")
         Next
        
    EndSwitch
Until $y = 1

As you can see, the same array "$rs[5][1]" is the button name, i want to check if the user has pressed on the button by Switch Case. But i cant create the Cases in the Switch function. Does anyone has a alternative ore a solution?

Tnx!

Share this post


Link to post
Share on other sites



One idea and one suggestion:

Idea: Write out each button name with it's own "Case" statement.

Suggestion: I would just put the For ... Next loop right after the switch statement. That's what I have done in similar situations.


My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

Why is there a For/Next loop here...?

$msg = GUIGetMsg()
    Switch $msg
       
         For $i = 1 To $rsn[0]
            Case $rs[5][1]
            msgBox(0,"","")
         Next
       
    EndSwitch

Either $msg = $rs[5][1], or not. Why check it more than once and pop an annoying MsgBox() when nothing changes for each itteration. What's the point?

:)


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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Why is there a For/Next loop here...?

$msg = GUIGetMsg()
    Switch $msg
       
         For $i = 1 To $rsn[0]
            Case $rs[5][1]
            msgBox(0,"","")
         Next
       
    EndSwitch

Either $msg = $rs[5][1], or not. Why check it more than once and pop an annoying MsgBox() when nothing changes for each itteration. What's the point?

:)

$rs[5][1] is a var its not constant. As you can see in the GUI every buttons gets a own name, wich i get out of a INI. But every $rs[5][1] needs a seperate function. How can i implmentate that in the Case function? Edited by xzaz

Share this post


Link to post
Share on other sites

Based on what you are saying above, why not just use an If/Then statement?...

$msg = GUIGetMsg()
For $iCC = 1 To $rsn[0]
    If $iCC = 5 And $msg = $rs[$iCC][0] Then
         ;$rs[5][0] function here
    ElseIf $msg = $rs[$iCC][0]
        ;Do something
    EndIf
Next
P.S.... This is totally confusing.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Tnx, i'll mess with that script.

I know its confusing, but its basicly very simpel only hard to program.

Out of the INI file he checks howmany ($rsn[0]) buttons there have to be created. Every Button gets his own var ($Button_x in this case its in the $rs[5][1] var what comes straight out of the INI Read). So the Case statement have to be for every button ($Button_x to $button_x there are $rsn[0] to make) make a action, so every button has its own function.

Share this post


Link to post
Share on other sites

I think I know what he wants, he wants to dynamically structure a case statement for ... whatever reason.... but

No, this can't be done. The logic just doesn't work that way. (and probably for a good reason...there's enough spaghetti in Italy for all of us without this logic)


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

I think I know what he wants, he wants to dynamically structure a case statement for ... whatever reason.... but

No, this can't be done. The logic just doesn't work that way. (and probably for a good reason...there's enough spaghetti in Italy for all of us without this logic)

;) Sad to hear, puzzeling a hour and a half for something that cant be done. Well i'll uploaded switch_case.rar the complete script. So if you wanna take a look you can now and see what i mean :)

Is there another way to check if the button has been pressed?

Share this post


Link to post
Share on other sites

Uh... yeah. If you've made your own GUI, then the line reading $nMsg = GUIGetMsg() contains the handle of whatever control has just been knocked by the user.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

:P Sad to hear, puzzeling a hour and a half for something that cant be done. Well i'll uploaded switch_case.rar the complete script. So if you wanna take a look you can now and see what i mean :)

Is there another way to check if the button has been pressed?

This demo puts all the button's controlIDs in an array, along with an index to determine what each button does. By changing the $iNumButtons variable, you can have any number of buttons:

#include <guiconstants.au3>

$iNumButtons = 12 ; How many buttons do you want?

; $Buttons array contains all the buttons in the GUI
;   [0][0] = count
;   [n][0] = ControlID of button
;   [n][1] = 0-based index to what button do
Global $avButtons[$iNumButtons + 1][2] = [[$iNumButtons, ""]]

; Create GUI
$hGUI = GUICreate("Test", 200, 10 + 40 * $avButtons[0][0])

; Create some buttons
For $n = 1 To $avButtons[0][0]
    $avButtons[$n][0] = GUICtrlCreateButton("Button: " & $n, 50, 10 + (($n - 1) * 40), 100, 30)
    $avButtons[$n][1] = Mod($n, 4) ; Trick to give buttons 4 different functions
Next

; Show GUI
GUISetState()
While 1
    $Msg = GUIGetMsg()
    If $Msg = $GUI_EVENT_CLOSE Then Exit
    
    ; See if a button was clicked and run its associated function
    For $n = 1 To $avButtons[0][0]
        If $Msg = $avButtons[$n][0] Then
            Switch $avButtons[$n][1]
                Case 0 
                    _ButtonFuncEven($n, "Zero")
                Case 1 
                    _ButtonFuncOdd($n, "One")
                Case 2
                    _ButtonFuncEven($n, "Two")
                Case 3 
                    _ButtonFuncOdd($n, "Three")
            EndSwitch
            ExitLoop
        EndIf
    Next
WEnd

Func _ButtonFuncEven($iInt, $sString)
    MsgBox(64, "Even function", "You clicked an even button " & $iInt & " = function type: " & $sString, 3)
EndFunc

Func _ButtonFuncOdd($iInt, $sString)
    MsgBox(64, "Odd function", "You clicked an odd button " & $iInt & " = function type: " & $sString, 3)
EndFunc

;)


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

Share this post


Link to post
Share on other sites

Ouch, but that kind of logic is rather impractical for a large scale GUI since this would definately slow it down, no?


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

Ouch, but that kind of logic is rather impractical for a large scale GUI since this would definately slow it down, no?

It's easier to make them on the fly using the array, and for that, you'd need to check them dynamically like this if the size of the array was unknown.

But I agree that if the size of the array is always know, there is no sense in doing anything but a simple Switch statement.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#include <guiconstants.au3>

$iNumButtons = 12 ; How many buttons do you want?

; $Buttons array contains all the buttons in the GUI
;   [0][0] = count
;   [n][0] = ControlID of button
;   [n][1] = 0-based index to what button do
Global $avButtons[$iNumButtons + 1][2] = [[$iNumButtons, ""]]

; Create GUI
$hGUI = GUICreate("Test", 200, 10 + 40 * $avButtons[0][0])

; Create some buttons
For $n = 1 To $avButtons[0][0]
    $avButtons[$n][0] = GUICtrlCreateButton("Button: " & $n, 50, 10 + (($n - 1) * 40), 100, 30)
    $avButtons[$n][1] = Mod($n, 4) ; Trick to give buttons 4 different functions
Next

; Show GUI
GUISetState()
While 1
    $Msg = GUIGetMsg()
    If $Msg = $GUI_EVENT_CLOSE Then Exit
   
    ; See if a button was clicked and run its associated function
    For $n = 1 To $avButtons[0][0]
        If $Msg = $avButtons[$n][0] Then
            Switch $avButtons[$n][1]
                Case 0
                    _ButtonFuncEven($n, "Zero")
                Case 1
                    _ButtonFuncOdd($n, "One")
                Case 2
                    _ButtonFuncEven($n, "Two")
                Case 3
                    _ButtonFuncOdd($n, "Three")
            EndSwitch
            ExitLoop
        EndIf
    Next
WEnd

Func _ButtonFuncEven($iInt, $sString)
    MsgBox(64, "Even function", "You clicked an even button " & $iInt & " = function type: " & $sString, 3)
EndFunc

Func _ButtonFuncOdd($iInt, $sString)
    MsgBox(64, "Odd function", "You clicked an odd button " & $iInt & " = function type: " & $sString, 3)
EndFunc

How can i get this to work? Tnx for all the help and input so far guys Appreciate it!

Share this post


Link to post
Share on other sites

Ouch, but that kind of logic is rather impractical for a large scale GUI since this would definately slow it down, no?

How are you going to make ANY message loop quicker for "a large scale GUI"?

I was trying to make it work in his world. The only reason I can see for the nonsensical loop he had was the expectation of a dynamic GUI where the number of controls, or the functions they perform must be changed while it's running. That requires keeping the ControlIDs in an array.

As for me: I would have converted the whole thing to GuiOnEventMode for my own script.

:)


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

Share this post


Link to post
Share on other sites

Using this for @Scriptdir & "\progs.ini":

[Sec_1]
Button=Button One

[Sec_2]
Button=Button Two

[Sec_3]
Button=Button Three

[Sec_4]
Button=Button Four

[Sec_5]
Button=Button Five

[Sec_6]
Button=Button Six

[Sec_7]
Button=Button Seven

[Sec_8]
Button=Button Eight

This demo works:

#include <guiconstants.au3>

$path = @ScriptDir & "\progs.ini" 
Global $avSecNames = IniReadSectionNames($path) ; Count Programs from INI
If @error Or $avSecNames[0] < 1 Then Exit

; $Buttons array contains all the buttons in the GUI
;   [0][0] = count
;   [n][0] = ControlID of button
;   [n][1] = 0-based index to what button do
Global $avButtons[$avSecNames[0] + 1][2] = [[$avSecNames[0], ""]] ; One button for each section

; Create GUI
$hGUI = GUICreate("Test", 200, 10 + 40 * $avButtons[0][0])

; Create some buttons
For $i = 1 To $avSecNames[0]
    $avReadSec = IniReadSection($path, $avSecNames[$i])
    If @error = 0 And $avReadSec[0][0] > 0 Then
        $avButtons[$i][0] = GUICtrlCreateButton($avReadSec[1][1], 50, 10 + (($i - 1) * 40), 100, 30)
        $avButtons[$i][1] = Mod($i, 4) ; Trick to give buttons 4 different functions
    EndIf
Next

; Show GUI
GUISetState()

While 1
    $Msg = GUIGetMsg()
    If $Msg = $GUI_EVENT_CLOSE Then Exit

    ; See if a button was clicked and run its associated function
    For $n = 1 To $avButtons[0][0]
        If $Msg = $avButtons[$n][0] Then
            Switch $avButtons[$n][1]
                Case 0
                    _ButtonFuncEven($n, "Zero")
                Case 1
                    _ButtonFuncOdd($n, "One")
                Case 2
                    _ButtonFuncEven($n, "Two")
                Case 3
                    _ButtonFuncOdd($n, "Three")
            EndSwitch
            ExitLoop
        EndIf
    Next
WEnd

Func _ButtonFuncEven($iInt, $sString)
    MsgBox(64, "Even function", "You clicked an even button " & $iInt & " = " & _
            ControlGetText($hGUI, "", $avButtons[$iInt][0]) & ", function type: " & $sString, 3)
EndFunc   ;==>_ButtonFuncEven

Func _ButtonFuncOdd($iInt, $sString)
    MsgBox(64, "Odd function", "You clicked an odd button " & $iInt & " = " & _
            ControlGetText($hGUI, "", $avButtons[$iInt][0]) & ", function type: " & $sString, 3)
EndFunc   ;==>_ButtonFuncOdd

:)


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

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
Sign in to follow this  
Followers 0