Page 1 of 1

Create arrays of textboxes etc

Posted: Wed Sep 28, 2011 4:04 am
by michaelb16561
Hi guys,

This is a small snippet of code to show how to create an array of textboxes and sliders etc.

Code: Select all// Declare the arrays of textboxes and sliders here so they can be accessed globally
var txtTextbox= [];
var sldSlider = [];

main();

function main(){
   var pnlPanel = [];
   var grpGroup = [];

   dlg = new Window ("dialog");
   
   // Add a panel and create the input controls
   
   for (var i = 0; i < 5; i++){
      pnlPanel = dlg.add('panel', undefined, 'Panel ' + (i + 1) + ':');
      
      grpGroup = pnlPanel.add('group');
      
      txtTextbox = grpGroup.add('edittext', undefined, '0');
      txtTextbox.preferredSize = [50,20];
      txtTextbox.justify = 'right';
      txtTextbox.text = 0;
      
      sldSlider = grpGroup.add('slider', undefined, 0, 0, 100);
   }

   // Show the window
   dlg.center();
   dlg.show();
   
   return;
}

Note: If the textboxes and sliders are to be accessed from another function it is required to delcare them as global variables.
For some reason arrays of textboxes, sliders etc don't seem able to be referenced as children of another object (eg. dlg.txtTexbox)

I'm still not quite sure how to assign event handlers to the controls dynamically, within the 'for loop'
This doesn't work:-
Code: Select alltxtTextbox[i].onChange = function () { sldSlider[i].value = Number(txtTextbox[i].text); };

You just have to assign each one individually:-
Code: Select alltxtTextbox[0].onChange = function () { sldSlider[0].value = Number(txtTextbox[0].text); };

Hope someone may find this helpful,

- Michael B

Re: Create arrays of textboxes etc

Posted: Mon Oct 03, 2016 10:09 pm
by michalt
I found the solution for assigning values dynamicly

Code: Select all


for (var i = 0; i < 5; i++){
txtTextbox[i].onChange = function () {
for (var k = 0; k < 5; k++){
sldSlider[k].value = Number(txtTextbox[k].text); }
}
}

Re: Create arrays of textboxes etc

Posted: Tue Oct 04, 2016 7:10 pm
by Kukurykus
After 5 years and 5 days?! It had to be long travel thru scripting lands :shock:

I'm joking, I'm learning and that is amazing news to me. Could you please show us how to implement that snippet to your earlier code that it really work dinamically, Thank You!