# Add elements from code

**URL:** <https://forum.servoy.com/t/add-elements-from-code/15770>\
**Category:** Classic Servoy\
**Created:** [April 3, 2012, 3:47pm UTC](https://forum.servoy.com/t/add-elements-from-code/15770 "2012-04-03T15:47:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DEK4](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@DEK4](https://forum.servoy.com/u/DEK4)\
**Post date:** [April 3, 2012, 3:47pm UTC](https://forum.servoy.com/t/add-elements-from-code/15770/1 "2012-04-03T15:47:55Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![Joas](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/joas/32/5095_2.png) [@Joas](https://forum.servoy.com/u/Joas)\
**Post date:** [April 5, 2012, 2:51pm UTC](https://forum.servoy.com/t/add-elements-from-code/15770/2 "2012-04-05T14:51:12Z")

</div>

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():

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![DEK4](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@DEK4](https://forum.servoy.com/u/DEK4)\
**Post date:** [April 6, 2012, 9:33am UTC](https://forum.servoy.com/t/add-elements-from-code/15770/3 "2012-04-06T09:33:29Z")

</div>

Thanks but it doesn’t work … 😕

here is my solution can you test it please?

i can’t understand where the error is 😕

[creaFinestra.servoy](https://forum.servoy.com/uploads/short-url/zuViUb0tkHxCEYIwUhtZEAUtggL.servoy) (4.37 KB)

---

<div class="post-metadata">

**Author:** ![Joas](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/joas/32/5095_2.png) [@Joas](https://forum.servoy.com/u/Joas)\
**Post date:** [April 6, 2012, 12:10pm UTC](https://forum.servoy.com/t/add-elements-from-code/15770/4 "2012-04-06T12:10:26Z")

</div>

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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![DEK4](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@DEK4](https://forum.servoy.com/u/DEK4)\
**Post date:** [April 6, 2012, 1:29pm UTC](https://forum.servoy.com/t/add-elements-from-code/15770/5 "2012-04-06T13:29:05Z")

</div>

Thanks a lot! ![:D]( "Very Happy")
