Add elements from code

Questions and answers for designing and implementing forms in Servoy

Add elements from code

Postby DEK4 » Tue Apr 03, 2012 5:47 pm

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
User avatar
DEK4
 
Posts: 111
Joined: Wed Oct 12, 2011 9:26 am

Re: Add elements from code

Postby Joas » Thu Apr 05, 2012 4:51 pm

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();
}
Joas de Haan
Yield Software Development
Need help on your project? yieldsd.com
User avatar
Joas
Site Admin
 
Posts: 842
Joined: Mon Mar 20, 2006 4:07 pm
Location: Leusden, NL

Re: Add elements from code

Postby DEK4 » Fri Apr 06, 2012 11:33 am

Thanks but it doesn't work ... :/

here is my solution can you test it please?

i can't understand where the error is :/
Attachments
creaFinestra.servoy
(4.37 KiB) Downloaded 175 times
User avatar
DEK4
 
Posts: 111
Joined: Wed Oct 12, 2011 9:26 am

Re: Add elements from code

Postby Joas » Fri Apr 06, 2012 2:10 pm

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.
Joas de Haan
Yield Software Development
Need help on your project? yieldsd.com
User avatar
Joas
Site Admin
 
Posts: 842
Joined: Mon Mar 20, 2006 4:07 pm
Location: Leusden, NL

Re: Add elements from code

Postby DEK4 » Fri Apr 06, 2012 3:29 pm

Thanks a lot! :D
User avatar
DEK4
 
Posts: 111
Joined: Wed Oct 12, 2011 9:26 am


Return to Forms

Who is online

Users browsing this forum: Google [Bot] and 1 guest