Sign in to follow this  
Followers 0
APRES

Case in loop

6 posts in this topic

Is it possible to join multiple Case inside of GUI into loop?

Example

While 1

$msg = GUIGetMsg()

Select

Case $msg = $arrayitem[1]

DoSomething(1)

Case $msg = $arrayitem[2]

DoSomething(2)

Case $msg = $arrayitem[3]

DoSomething(3)

Case $msg = $arrayitem[4]

DoSomething(4)

........................

Case $msg = $arrayitem[999]

DoSomething(999)

Case $msg = $arrayitem[1000]

DoSomething(1000)

So there is a temptation to do something like

For $i=1 To 1000

Case $msg = $arrayitem[$i]

DoSomething($i)

Next

I tried use Eval or Execute but didn't succeed. Is it possible to do?

Thank!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Is it possible to join multiple Case inside of GUI into loop?

I tried use Eval or Execute but didn't succeed. Is it possible to do?

Thank!

Why use a case?

For $i=1 To 1000
    if $msg = $arrayitem[$i] then 
        DoSomething($i)
        ExitLoop
    endif
Next

EDIT: do you have 1000 GUI Controls"?

Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Why use a case?

For $i=1 To 1000
    if $msg = $arrayitem[$i] then 
        DoSomething($i)
        ExitLoop
    endif
Next

Don't know. Had read somewhere that "Case" is faster then "If...Then"

:)

But thanks anyhow. It works indeed. And the code became a lot shorter. :P

do you have 1000 GUI Controls"?

Of course not. It was just a shocking example :D

Share this post


Link to post
Share on other sites

... EDIT: do you have 1000 GUI Controls"?

and what if i do have "1000" GUI controls? ...and i can not use other control:

#include <GUIConstants.au3>

GUICreate("My GUI menu", 300, 200)

$menuLng = GUICtrlCreateMenu("Language")

$item = "sk,cz,de"
$item = StringSplit($item, ",")

; Create variables
For $i = 1 To $item[0]
    $menuEntry = $item[0]
    Assign("item_" & $item[$i], GUICtrlCreateMenuitem($item[$i], $menuLng, $menuEntry, 1))
    $menuEntry = $menuEntry - 1
Next

GUISetState()


While 1
    $msg = GUIGetMsg()

    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
            
        #cs --> this should be done in loop:
        Case $item_sk
            MsgBox(0, "", GUICtrlRead($item_sk, 1))
        
        Case $item_cz
            MsgBox(0, "", GUICtrlRead($item_cz, 1))
        
        Case $item_de
            MsgBox(0, "", GUICtrlRead($item_de, 1))
        #ce
            
    EndSwitch

WEnd

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

and what if i do have "1000" GUI controls? ...and i can not use other control:

No idea what you mean by this statement..

But as far as your script: use an Array in stead of Assign command. That will allow you to loop through the defined number of controls.

Try this example

:shocked:

#include <GUIConstants.au3>
GUICreate("My GUI menu", 300, 200)
$menuLng = GUICtrlCreateMenu("Language")
$item = "sk,cz,de"
$item = StringSplit($item, ",")
; Create variables
Dim $Controls[$item[0]]
For $i = 1 To $item[0]
    $menuEntry = $item[$i]
    $Controls[$i-1] = GUICtrlCreateMenuItem($item[$i], $menuLng, $i -1, 1)
Next
GUISetState()
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    For $i = 0 To UBound($Controls)-1
        If $msg = $Controls[$i] Then
            MsgBox(0, "", GUICtrlRead($Controls[$i], 1))
        EndIf
    Next
WEnd
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

great! that is exactly what i wanted... many thanks!

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