Page 1 of 1

Add elements from code

PostPosted: Tue Apr 03, 2012 5:47 pm
by DEK4
Hello!

I want to add elements like labels, field and other from the code creating a new form.

I try this but it didn't work:
Code: Select all
function onAction(event) {
   var sForm = "testForm"
   history.removeForm(sForm);
   controller.recreateUI();
   
   // Get the form
   var password = "ciao"
   var myForm = solutionModel.newForm("asd",null,null,null,false,500,500)
   myForm.newLabel('Test', 20, 20, 120, 30)
   myForm.newLabel("ASDADASDASDASDASDASD",150,150,500,400).background='green'
   myForm.newPassword(password,200,100,150,50)
}

I create a new empty form called "testForm", and when i click to the button i want to populate this form with my fields...how can i do this?

Thanks

Re: Add elements from code

PostPosted: Thu Apr 05, 2012 4:51 pm
by Joas
So you want to change your existing form "testForm", correct?
In that case you should use the function solutionModel.getForm() to get the formObject, then make your changes and in the end call controller.recreateUI():

Code: Select all
function onAction(event) {
   var sForm = "testForm";
   
   // Get the form
   var password = "ciao";
   var myForm = solutionModel.getForm(sForm);
   myForm.newLabel('Test', 20, 20, 120, 30);
   myForm.newLabel("ASDADASDASDASDASDASD",150,150,500,400).background='green';
   myForm.newPassword(password,200,100,150,50);
   controller.recreateUI();
}

Re: Add elements from code

PostPosted: Fri Apr 06, 2012 11:33 am
by DEK4
Thanks but it doesn't work ... :/

here is my solution can you test it please?

i can't understand where the error is :/

Re: Add elements from code

PostPosted: Fri Apr 06, 2012 2:10 pm
by Joas
Whenever you change something with the solutionModel() you have to call recreateUI(), otherwise you get the error "Stale form(s) detected" when you are changing the form again.
In the onLoad method of your main-form, you don't do that.

Also note that you don't have to remove the form from history anymore before making changes with the solutionModel. Since the introduction of the recreateUI() function, that isn't necessary anymore.

So if you change the onLoad method on your main form to this:
Code: Select all
function onLoad(event) {
   var __sForm = forms.testForm.controller.getName();
   
   var _form = solutionModel.getForm(__sForm)
   
   //Create form
   _form.navigator = SM_DEFAULTS.NONE;
   _form.newLabel("Test label",10,10,100,30);
   var _labelVar = "text";
   _form.newField(_labelVar,JSField.TEXT_FIELD,50,50,100,50);
   forms[__sForm].controller.recreateUI();
   forms[__sForm].controller.show(_form);
}

and the _btnAddElementmethod on your testForm to this:
Code: Select all
function _btnAddElement(event) {
   var __sForm = event.getFormName();
   application.output(__sForm);
   var _form = solutionModel.getForm(__sForm);
   
   _form.newLabel("New label insert with the button!",40,40,300,20);
   var something = "asd";
   _form.newField(something,JSField.TEXT_FIELD,60,60,200,30);
   controller.recreateUI();
}

Then it works correct.

Re: Add elements from code

PostPosted: Fri Apr 06, 2012 3:29 pm
by DEK4
Thanks a lot! :D