ScriptSpot

Forum topic · General Scripting

Get name item definition (label) with a loop (for)

By Michele71 · 2015-04-22

Description

Hi all :)

A my friend asked me if is possible get a name of a label and change the label string with a loop. I have try this code:


rollout Test "test" width:100 height:200
(         
label temp1 "hello1"         
label temp2 "hello2"         

on Test open do         
(                  
for i=1 to 2 do                  
(            
local namelabel = "temp" + i as string                           
namelabel.text = "changetext" + i as string                  
)          
)
)
createDialog Test

I have always the same error : Unknown property: "text" in "temp1"

How to access the item definition (temp1/temp2) of label then update the value string ? I can do this?

Thanks for any help

Comments (6)

victorgilbert · 2015-05-08

Very nice explanation. I really got excited for the first look with the code. I use to write the similar kind of code in frequent times.

`

pixamoon · 2015-04-23

hey again :)

just small thing:

You need to format it and execute it :)


(
try(destroyDialog ::Test)catch()
rollout Test "test" width:100 height:200
	(         
		label temp1 "hello1"         
		label temp2 "hello2"         
		 
		on Test open do (                  
			for i=1 to 2 do                  
				(            
					local str = stringStream ""
					format "Test.temp%.text = \"%\" + % as string" i "changetext" i to:str     
					execute(str as string)
				)          
		)
	)
createDialog Test
)

It should works now,

Michele71 · 2015-04-23

Thanks pixamoon. I tried with the format, but without execute it! hahahahah. I see if it works!

Thanks again for support and patience

`

pixamoon · 2015-04-23

no prob, anytime :)

barigazy · 2015-04-26

One more example

(
try(destroyDialog ::Test)catch()
rollout Test "test" width:200 height:100
(
local lblNames = #("John", "Stalone", "BruceLee", "HELLOOOO", "ZZZZzzzz", "Michael", "71")
label temp1 "hello1" width:100 height:20 style_sunkenedge:on
label temp2 "hello2" width:100 height:20 style_sunkenedge:on

button btn "Random Names" width:150
on btn pressed do for i = 1 to 2 do test.controls[i].caption = lblNames[random 1 lblNames.count]
)
createDialog Test
)

Michele71 · 2015-04-26

Thanks for this good example Barigazy! Thanks again :)