Notifications
Clear all

[Closed] multiple slider/button enabling

a simple one im sure and i thought i read a problem like this a while ago but i cant remember if it was solved, nor can i find it through the search!

I have 15 button and five sliders, for arguments sake called sldr1-sldr5 and but1-but15

rather that type out sldr1.enabled = true…sldr2.enabled = true etc etc etc i would have thought you could make that a bit more elegant in a for loop rather than the 20 lines of code each time i want to change the state. is this possible?

8 Replies

Definately posible with a loop and an execute command, just have to make sure you address the controls properly.

-------------------------------------------------
-------------------------------------------------
-- excuteExample
-- written by: Thomas Blue
-- history:
--	Tuesday, August 29, 2006 - started development
--
-- TODO:
--
-------------------------------------------------
-------------------------------------------------
global excuteExampleROLL
(
	 try(DestroyDialog excuteExampleROLL)catch()
	 -------------------------------------------------
	 -- main ui
	 -------------------------------------------------
	 rollout excuteExampleROLL "Excute Example" width:300 height:400
	 (
		 button enableBTN "Enable" width:130 across:2
		 button disableBTN "Disable" width:130
		 label blankLBL ""
		 button btn1 "Button" width:130 across:2
		 button btn2 "Button" width:130
		 button btn3 "Button" width:130 across:2
		 button btn4 "Button" width:130
		 button btn5 "Button" width:130 across:2
		 button btn6 "Button" width:130
		 button btn7 "Button" width:130 across:2
		 button btn8 "Button" width:130
		 button btn9 "Button" width:130 across:2
		 button btn10 "Button" width:130
		 button btn11 "Button" width:130 across:2
		 button btn12 "Button" width:130
		 slider sld1 "Slider" width:130 height:44 across:2
		 slider sld2 "Slider" width:130 height:44
		 slider sld3 "Slider" width:130 height:44 across:2
		 slider sld4 "Slider" width:130 height:44
		 slider sld5 "Slider" width:130 height:44 across:2
		 slider sld6 "Slider" width:130 height:44
	 
		 on enableBTN pressed do
		 (
			 for i = 1 to 12 do execute ("excuteExampleROLL.btn" + i as string + ".enabled = true")
			 for i = 1 to 6 do execute ("excuteExampleROLL.sld" + i as string + ".enabled = true")
		 )
		 on disableBTN pressed do
		 (
			 for i = 1 to 12 do execute ("excuteExampleROLL.btn" + i as string + ".enabled = false")
			 for i = 1 to 6 do execute ("excuteExampleROLL.sld" + i as string + ".enabled = false")
		 )
	 )
	 createdialog excuteExampleROLL style:#(#style_minimizebox, #style_titlebar, #style_sysmenu, #style_border)
)

Awesome stuff Thomas! I had heard of the execute command but wasn’t sure how to use it. I wouldn’t have thought of the need to define the

rollout.sld1 etc...

in that syntax either. I had stored the controlls in an array but kept getting “undefined” back when i attempted the loop.

so thanks, i would have been stuck on that. As my sliders call external structs it has made my whole script very compact indeed! many thanks!

 PEN

In my Auto Material script I had to enable and disable most of the UI when a certain check button was checked. Instead of running a loop which I couldn’t do the same way as the uhi item names were all very different I opted to not change the state of certain UI items. I used a for loop and theRolloutName.controls which will return all the controls in the rollout. I then had an array of the 5 names of UI items that I didn’t want to turn off and I tested and skipped those.

You mentioned that you didn’t want to write out the same 20 lines each time you want to change the state? Are you using lots of functions? You should only have to do it once and then call the function when you want to make the change.

Personally I prefer to make arrays of associated controls, ones that I will enable/disable at the same time, and run through those arrays when needed. It saves on quite a few conditionals and it is not dependant on the order of your controls.

Just another approach.

I use a for loop over an array of controls as in: for i in #(…) do i.enabled = true

Light

good stuff chaps, many thanks. When i was thinking how to do it, i tried an array of controls too but i kept getting an error. do you have to define them as rollout.controlname rather than controlname inside the array?

on a side note, I have been using structs for the first time this week and its really opened my eyes to a new level of data handling. awesome!

Controlname should be fine as the array is defined inside the scope of the rollout definition.

Light

Cheers Light, duly noted.