# onFocusGained in a runtime created form

**URL:** <https://forum.servoy.com/t/onfocusgained-in-a-runtime-created-form/18239>\
**Category:** Classic Servoy\
**Created:** [November 4, 2014, 8:30am UTC](https://forum.servoy.com/t/onfocusgained-in-a-runtime-created-form/18239 "2014-11-04T08:30:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![studiomiazzo](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@studiomiazzo](https://forum.servoy.com/u/studiomiazzo)\
**Post date:** [November 4, 2014, 8:30am UTC](https://forum.servoy.com/t/onfocusgained-in-a-runtime-created-form/18239/1 "2014-11-04T08:30:29Z")

</div>

If I dynamically create a form with the solutionModel, I can’t get the onFocusGained and onFocusLost methods work on a field element. The weird thing is that the onAction and onDataChange methods bounded to the same field element instead work correctly. Have anybody faced the same problem? I’m using Servoy 7.4.1 but I’ve also tried in 7.4.2 and in the previous release with the same result.

Here’s a example of my code returning the form created :

```auto
	var tForm = solutionModel.newForm(formName, globals.Server.MA_PRESENZE, globals.Table.STORICO, 'leaf_style', false, 360, 150);
		tForm.extendsForm = solutionModel.getForm(forms.svy_nav_base.controller.getName());
		tForm.navigator = SM_DEFAULTS.NONE;
		tForm.view = JSForm.RECORD_VIEW;
		tForm.scrollbars = SM_SCROLLBAR.HORIZONTAL_SCROLLBAR_NEVER | SM_SCROLLBAR.VERTICAL_SCROLLBAR_NEVER;
		tForm.transparent = true;
    
	/** @type JSLabel */
	var tLabel
	/** @type JSField */
	var tField
	/** @type {JSVariable} */
	var tVariable;
	
	var topMargin = 0;
	var sideMargin = 10;
	
	var fldHeight = 20;
	var fldLength = 80;
	var lblHeight = 20;
	var lblLength = 170;
	
	var xLLeft = sideMargin;
	var xLRight = tForm.width - sideMargin - lblLength;//Math.floor(tForm.width / 2) + xLLeft;
	
	var dy = lblHeight + fldHeight;	

	var fieldType;
	var fieldStyle = 'default';
	var fieldFormat = '';
	var fieldAlignment = SM_ALIGNMENT.CENTER;
	var fieldsNo = 10;	
	for (var i = 1; i <= fieldsNo; i++) 
	{
		var dataType = globals.DataType.DATETIME;
		var variableType = = JSVariable.DATETIME;
		tVariable = tForm.newVariable('v' + currField.codice, variableType);
		var row = Math.floor((i - 1) / 2);
		
		tField.name = currField.codice;
		tField.styleClass = fieldStyle;
		tField.format = fieldFormat;
		tField.horizontalAlignment = fieldAlignment;
		tField.transparent = transparent;
		tField.tabSeq = i;
		
		
		tField.onDataChange = solutionModel.getGlobalMethod('globals','onFocusGainedLost');
		tField.onAction = solutionModel.getGlobalMethod('globals','onActionFocusGainedLost');

                tField.onFocusGained = solutionModel.getGlobalMethod('globals','onFocusGained');
		tField.onFocusLost = solutionModel.getGlobalMethod('globals','onFocusLost');
		
	}
	
	return tForm;
}

```

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [November 24, 2014, 5:03pm UTC](https://forum.servoy.com/t/onfocusgained-in-a-runtime-created-form/18239/2 "2014-11-24T17:03:31Z")

</div>

that should work fine, are you sure you are attaching a good global method?

There should not be any difference, so you add the same exact method to onaction,ondatachange and the 2 onfocus  
then focus is never called?

---

<div class="post-metadata">

**Author:** ![studiomiazzo](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@studiomiazzo](https://forum.servoy.com/u/studiomiazzo)\
**Post date:** [November 25, 2014, 8:02am UTC](https://forum.servoy.com/t/onfocusgained-in-a-runtime-created-form/18239/3 "2014-11-25T08:02:24Z")

</div>

Sorry, I’ll try to explain with a more understandable code, imagine we have :

```auto
      tField.onDataChange = solutionModel.getGlobalMethod('globals','testMethod');
      tField.onAction = solutionModel.getGlobalMethod('globals','testMethod');

      tField.onFocusGained = solutionModel.getGlobalMethod('globals','testMethod');
      tField.onFocusLost = solutionModel.getGlobalMethod('globals','testMethod');

```

Function _testMethod_ is reached on the onAction and onDataChange but is never reached due to gained or lost focus.

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [November 25, 2014, 9:29am UTC](https://forum.servoy.com/t/onfocusgained-in-a-runtime-created-form/18239/4 "2014-11-25T09:29:39Z")

</div>

that should just work, make a reproducible example and attach that to a issue in our jira
