# Removing Form in solutionModel

**URL:** <https://forum.servoy.com/t/removing-form-in-solutionmodel/15213>\
**Category:** Classic Servoy\
**Created:** [December 20, 2011, 9:48pm UTC](https://forum.servoy.com/t/removing-form-in-solutionmodel/15213 "2011-12-20T21:48:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gldni](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@gldni](https://forum.servoy.com/u/gldni)\
**Post date:** [December 20, 2011, 9:48pm UTC](https://forum.servoy.com/t/removing-form-in-solutionmodel/15213/1 "2011-12-20T21:48:12Z")

</div>

I’ve used the solutionModel in the past and haven’t had any problems, but for some reason this time around I’m having trouble removing my forms. When I click on a button, this code fires:

```auto
function loadMsgCenter(event)
{
	//Set globals.g_WindowFlag
	globals.g_WindowFlag = 0;
			
	//show form in window
	var md = application.createWindow('messageCenter',JSWindow.MODAL_DIALOG);
	md.title = 'Message Center';
	
	//Pass Window Name
	globals.g_WindowName = md.getName();
	
	forms.Message_Center3.controller.show(md);
}

```

The onShow of form Message\_Center3 looks like this:

```auto
function onShow()
{	
	//Declare Variables
	var success;
	var form;
	var groupQuery;
	var groupDataset;
	var show, close,newMsg;
	var methShow, methClose, methNew;
	var btnClose, btnNew;
	var variable;
	var btnBytes;
	var btnImage;
	var label;
	
	//Remove forms
	success = history.removeForm('Message_Buttons');
	if(success)
	{
		solutionModel.removeForm('Message_Buttons');
	}
	
	//Set up new solutionModel form
	form = solutionModel.newForm('Message_Buttons','eclinic','messages','eClinic',false,131,605);
	form.borderType = "EmptyBorder, 0, 0, 0, 0";
	form.navigator = SM_DEFAULTS.NONE;
	
	//Create form variable nameHold
	form.newFormVariable('nameHold',JSVariable.TEXT);
	
	
					  //Create Close Method
	close = "function close(event){";
					  
					  //call close window
	close += " globals.Close_Window();";
	
	close += " application.output(history.removeForm('Message_Buttons'));";
	close += " application.output(solutionModel.removeForm('Message_Buttons'));";
	
					  //Set window flag
	close += " globals.g_WindowFlag = 0;";
	close += "}";
	methClose = form.newFormMethod(close);
	
	//Create buttons	
	btnBytes = plugins.file.readFile('C:/users/nicholas/servoy_workspace6/eclinic/medias/close.png');
	btnImage = solutionModel.newMedia('close.jpg', btnBytes);
	btnClose = form.newLabel('',5,194,121,45,methClose);
	btnClose.imageMedia = btnImage;
	btnClose.rolloverCursor = SM_CURSOR.HAND_CURSOR;
	btnClose.borderType = "EmptyBorder, 0, 0, 0, 0";
	btnClose.transparent = true;
	btnClose.showClick = false;
	btnClose.showFocus = false;
	
	//set form in tab panel
	elements.tabs_Msg_Buttons.addTab(forms['Message_Buttons'],null,null,null,null,null,null,null,null);
	
	//Set tab visibility
	elements.tab_inboxView.visible = false;
}

```

The global method Close\_Window() that is called by clicking the close button is this:

```auto
function Close_Window()
{
	//Declare Variables
	var win;
	
	//Set globals.g_WindowFlag
	globals.g_WindowFlag = 1;

	// close and dispose window resources
	win = application.getWindow(globals.g_WindowName);
	win.destroy();
	win = application.getWindow(globals.g_WindowName);
	if(win === null)
	{
		application.output("Window '" + globals.g_WindowName + "' has been destroyed");
		globals.g_WindowName = null;
	}
	else
	{ 
		application.output("Window " + globals.g_WindowName + " could not be destroyed");
	}
}

```

Also, the form Message\_Center3 has an onHide method:

```auto
function ForminDialog_OnHide_Close()
{
//	globals.g_WindowFlag = 1
	if ( globals.g_WindowFlag != 1 ) 
	{ 
	   application.output('ForminDialog_OnHide_Close returns false'); 
	   return false; 
	} 
	else 
	{ 
// globals.g_WindowFlag = null ;
		application.output('ForminDialog_OnHide_Close returns true');
		return true;
	} 
}

```

It works great the first time I click the button that sets everything in motion. After I close the form and hit the button again I get this error:  
The name ‘Message\_Buttons’ already exists as another form  
Wrapped java.lang.RuntimeException: com.servoy.j2db.persistence.RepositoryException: The name ‘Message\_Buttons’ already exists as another form

I also noticed when debugging that the function ForminDialog\_OnHide\_Close() executes, then immediately executes again. Don’t know if that has anything to do with my issue.

I assume this means I’m touching the solutionModel form ‘Message\_Buttons’ in some way after the the win.destroy(), but I don’t see where. Can someone with a keen eye point out my mistake?

Thanks!

---

<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:** [December 21, 2011, 2:13pm UTC](https://forum.servoy.com/t/removing-form-in-solutionmodel/15213/2 "2011-12-21T14:13:41Z")

</div>

what does this return:

success = history.removeForm(‘Message\_Buttons’);  
if(success)  
{  
success = solutionModel.removeForm(‘Message\_Buttons’);  
}

is success really true there?

---

<div class="post-metadata">

**Author:** ![gldni](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@gldni](https://forum.servoy.com/u/gldni)\
**Post date:** [December 21, 2011, 5:03pm UTC](https://forum.servoy.com/t/removing-form-in-solutionmodel/15213/3 "2011-12-21T17:03:58Z")

</div>

It returns false. I forgot to mention that in my first post. I wan’t sure if destroying the window that contained the solutionModel form would remove it. I always use:

```auto
success = history.removeForm('Message_Buttons');
if(success)
{
success = solutionModel.removeForm('Message_Buttons');
}

```

at the beginning of my solutionModel code to avoid stale forms. But I can’t figure out why this form can’t be removed.

---

<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:** [December 22, 2011, 9:52am UTC](https://forum.servoy.com/t/removing-form-in-solutionmodel/15213/4 "2011-12-22T09:52:04Z")

</div>

if the form is visible on screen, then it will not be able to remove it.  
Visible forms or active forms can’t be removed.

---

<div class="post-metadata">

**Author:** ![gldni](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@gldni](https://forum.servoy.com/u/gldni)\
**Post date:** [December 22, 2011, 3:40pm UTC](https://forum.servoy.com/t/removing-form-in-solutionmodel/15213/5 "2011-12-22T15:40:32Z")

</div>

The win.destroy() closes/destroys the window that contains the form, so it isn’t on the screen. Unless the destroy() function isn’t what I need to do to make sure the form isn’t active. Is there something else I should use?

---

<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:** [December 22, 2011, 3:57pm UTC](https://forum.servoy.com/t/removing-form-in-solutionmodel/15213/6 "2011-12-22T15:57:00Z")

</div>

if you are sure that it isn’t active or visible, then you could create a case for this with a sample solution so that we can have a look

---

<div class="post-metadata">

**Author:** ![gldni](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@gldni](https://forum.servoy.com/u/gldni)\
**Post date:** [December 26, 2011, 10:55pm UTC](https://forum.servoy.com/t/removing-form-in-solutionmodel/15213/7 "2011-12-26T22:55:57Z")

</div>

I am sure the form isn’t visible, but I’m not sure it isn’t active. In the onshow code I reference in my first post, I have now tried to remove the solutionModel form ‘Message Buttons’ in the Close() method and added some application.outputs:

```auto
function onShow()
{   
   //Declare Variables
   var success;
   var form;
   var groupQuery;
   var groupDataset;
   var show, close,newMsg;
   var methShow, methClose, methNew;
   var btnClose, btnNew;
   var variable;
   var btnBytes;
   var btnImage;
   var label;
   
   //Remove forms
   success = history.removeForm('Message_Buttons');
   if(success)
   {
      solutionModel.removeForm('Message_Buttons');
   }
   
   //Set up new solutionModel form
   form = solutionModel.newForm('Message_Buttons','eclinic','messages','eClinic',false,131,605);
   form.borderType = "EmptyBorder, 0, 0, 0, 0";
   form.navigator = SM_DEFAULTS.NONE;
   
   //Create form variable nameHold
   form.newFormVariable('nameHold',JSVariable.TEXT);
   
   
                 //Create Close Method
   close = "function close(event){";
	
   close += " var histRemoved;";
   close += " var smRemoved;";
	
					  //call close window
   close += " globals.Close_Window();";
	
   close += " histRemoved = history.removeForm('Message_Buttons');";
   close += " application.output('Message_Buttons removed from history = ' + histRemoved);";
   close += " if(histRemoved){";
   close += " smRemoved = solutionModel.removeForm('Message_Buttons');";
   close += " application.output('Message_Buttons removed from SM= ' + smRemoved);";
   close += " }";
   
                 //Set window flag
   close += " globals.g_WindowFlag = 0;";
   close += "}";
   methClose = form.newFormMethod(close);
   
   //Create buttons   
   btnBytes = plugins.file.readFile('C:/users/nicholas/servoy_workspace6/eclinic/medias/close.png');
   btnImage = solutionModel.newMedia('close.jpg', btnBytes);
   btnClose = form.newLabel('',5,194,121,45,methClose);
   btnClose.imageMedia = btnImage;
   btnClose.rolloverCursor = SM_CURSOR.HAND_CURSOR;
   btnClose.borderType = "EmptyBorder, 0, 0, 0, 0";
   btnClose.transparent = true;
   btnClose.showClick = false;
   btnClose.showFocus = false;
   
   //set form in tab panel
   elements.tabs_Msg_Buttons.addTab(forms['Message_Buttons'],null,null,null,null,null,null,null,null);
   
   //Set tab visibility
   elements.tab_inboxView.visible = false;
}

```

My console reads as follows:  
ForminDialog\_OnHide\_Close returns true  
ForminDialog\_OnHide\_Close returns true  
Window ‘messageCenter’ has been destroyed  
Message\_Buttons removed from history = false

I don’t see how I’m still touching or referencing the form that would keep it from being removed. I could create a sample solution, but can’t be sure this is a bug over user error.

---

<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:** [January 2, 2012, 11:28am UTC](https://forum.servoy.com/t/removing-form-in-solutionmodel/15213/8 "2012-01-02T11:28:45Z")

</div>

if i see that correctly  
then “Message\_buttons”  
has a button with that onclose method and that onclose method tries to remove “message\_buttons”  
so it tries to remove itself, this is not allowed. Because then you are trying to destroy the context/scope where the method runs in. You can’t destroy “this”

---

<div class="post-metadata">

**Author:** ![gldni](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@gldni](https://forum.servoy.com/u/gldni)\
**Post date:** [January 2, 2012, 3:14pm UTC](https://forum.servoy.com/t/removing-form-in-solutionmodel/15213/9 "2012-01-02T15:14:17Z")

</div>

That is exactly what I was overlooking and needed pointed out. Thanks Johan!
