Sign in to follow this  
Followers 0
Damein

How to use a var in a For statement from a label?

2 posts in this topic

#1 ·  Posted (edited)

Not 100% sure how to ask this, so let me show you an example.

#include <GUIConstantsEx.au3>

GuiCreate("Test GUI")
$1 = GuiCtrlCreateLabel("This is a test label 1", 5,30)
$2 = GuiCtrlCreateLabel("This is a test label 2", 5,60)
$3 = GuiCtrlCreateLabel("This is a test label 3", 5,90)
GuiSetState()

While 1
    For $Count = 0 To 4 Step +1
        GuiCtrlSetData($Count, "Change to me!")
        Sleep(600)
        GuiCtrlSetData($Count, "Then back to original!")
        Sleep(600)
        GuiCtrlSetData($Count, "This is a test label " & $Count)
        Sleep(600)
    Next
    $Count = 0
    Sleep(10)
    WEnd

So in this script I would assume that on each go it would change one label, starting at 1, since Count would be the numeral 1 and the label has the numeral one as well. I may be assuming too much and the Count var does not actually call upon the $1 as I thought it would, if this is the case then is there a way to do this?

Thanks.

As a side note, running the script as is will change the first and second label, but will also display it as the count being 3 for 1 and 4 for 2 on the labels. So I'm not 100% what is going on.

Edited by Damein

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites



Damein,

I may be assuming too much

Correct! ;)

The variable names are $1, $2 and $3, while $Count is set to 1, 2 and 3. You need to use Execute like this:

#include <GUIConstantsEx.au3>

HotKeySet("{ESC}", "On_Exit")

GuiCreate("Test GUI")
$1 = GuiCtrlCreateLabel("This is a test label 1", 5, 30, 200)
$2 = GuiCtrlCreateLabel("This is a test label 2", 5, 60, 200)
$3 = GuiCtrlCreateLabel("This is a test label 3", 5, 90, 200)
GuiSetState()

While 1
    For $Count = 1 To 3
        GuiCtrlSetData(Execute("$" & $Count), "Change to me!")
        Sleep(600)
        GuiCtrlSetData(Execute("$" & $Count), "Then back to original!")
        Sleep(600)
        GuiCtrlSetData(Execute("$" & $Count), "This is a test label " & $Count)
        Sleep(600)
    Next
    $Count = 0
    Sleep(10)

WEnd

Func On_Exit()
    Exit
EndFunc

The Execute function returns the content of the variable defined by "$" & $count - which is the ControlID of the label.

However, the more elegant way to do this is using an array like this:

#include <GUIConstantsEx.au3>

HotKeySet("{ESC}", "On_Exit")

Global $aLabels[4]

GuiCreate("Test GUI")
$aLabels[1] = GuiCtrlCreateLabel("This is a test label 1", 5, 30, 200)
$aLabels[2] = GuiCtrlCreateLabel("This is a test label 2", 5, 60, 200)
$aLabels[3] = GuiCtrlCreateLabel("This is a test label 3", 5, 90, 200)
GuiSetState()

While 1
    For $Count = 1 To 3 Step +1
        GuiCtrlSetData($aLabels[$Count], "Change to me!")
        Sleep(600)
        GuiCtrlSetData($aLabels[$Count], "Then back to original!")
        Sleep(600)
        GuiCtrlSetData($aLabels[$Count], "This is a test label " & $Count)
        Sleep(600)
    Next

    $Count = 0
    Sleep(10)

WEnd

Func On_Exit()
    Exit
EndFunc

Now you do not need the clumsy Execute statements.

All clear? Ask if not. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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