Create arrays of textboxes etc
Posted: Wed Sep 28, 2011 4:04 am
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
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