Sign in to follow this  
Followers 0
geotau3

how to use loop FOR in Variable

12 posts in this topic

in other word 

if i have 

 $1= GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)

$2 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)

$3 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)

$4 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)

how can i use it in for  loop

For $i = 1 to 4

GUICtrlSetData ($i,12)

Next

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Use an array for storing the handles to the created controls:

GUICreate("Thingy")
Dim $aLabels[4]

For $i = 0 To 3 ; <-- array indices for an array dimensioned with four elements are 0-based, so they are: 0, 1, 2 and 3
    $aLabels[$i] = GUICtrlCreateLabel("Line " & $i & " Cell 1", 30, 10 + $i * 30, 300)
Next

GUISetState()

sleep(2000)

For $i = 0 To 3
    GUICtrlSetData($aLabels[$i], "replacement " & $i + 1)
    sleep (1000)
Next

 

Edited by SadBunny
typo in comment

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
$1= GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)
$2 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)
$3 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell
$4 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)

For $i = 1 to 4
    GUICtrlSetData(Eval($i), 12)
Next

Or

Global $hDummyStartLabels = GUICtrlCreateDummy()
$1= GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)
$2 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)
$3 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell
$4 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)
Global $hDummyEndLabels = GUICtrlCreateDummy()

For $i = $hDummyStartLabels + 1 to $hDummyEndLabels - 1
    GUICtrlSetData($i, 12)
Next

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This is very simple and doesn't require using Eval.

$1 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)

$2 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)

$3 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)

$4 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)

; how can i use it In For loop

For $i = $1 To $4

    GUICtrlSetData($i, 12)

Next

 

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

That works, but it leads to shaky code as its functionality will depend on the order in which the labels are created in the code, and whether or not there's any other things created in between those assignments.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

The whole script is shaky to start with.

Your script would work identically to what I wrote. You're building the controls in the exact same order that I am, you're just assigning the control IDs to an array element instead of simple variables. Control IDs in an array are equally dependent upon order of creation when building them in a loop.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Agreed, it would work identically, but once it grows and grows and grows like these scripts have a tendency to do, your approach is easier to break, e.g. if you intersperse the labels with inputs, buttons and other controls.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Then don't do that, it's not that hard to avoid stupid mistakes once you know what you're doing.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I won't. But TS might, and then wonder what the * is going on :) 


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

thanks 

but what if the variable contain more than number 

$GSa1 , $GSa2, $GSa3, $GSa4

Share this post


Link to post
Share on other sites

Then you can store the handles in an array, use eval if that's how your variables are formatted, or use Brew's method

$GSa1= GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)
$GSa2 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)
$GSa3 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell
$GSa4 = GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell)

$aLabels[] = [$GSa1, $GSa2, $GSa3, $GSa4]

For $i = 1 to 4
    GUICtrlSetData(Eval("GSa" & $i), 12)
Next

For $i = $GSa1 to $GSa4
    GUICtrlSetData($i, 12)
Next

For $i = 0 to UBound($aLabels) - 1
    GUICtrlSetData($aLabels[$i], 12)
Next

 

Share this post


Link to post
Share on other sites

geotau3,

When defining groups of variables it is always better to define them in a loop and store the ctlids in an array (post #2). 

@InunoTaishou - I've looked at your code.  I know you would never write goobledeegook like that.  I think it is generally good to offer alternative ways of approaching  problem, however, given this users relative inexperience I would stay away from "eval", "assign", "execute", etc.

$.02

kylomas


Forum Rules         Procedure for posting code

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