Removing Form in solutionModel

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:

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:

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:

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:

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!

what does this return:

success = history.removeForm(‘Message_Buttons’);
if(success)
{
success = solutionModel.removeForm(‘Message_Buttons’);
}

is success really true there?

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:

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.

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.

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?

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

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:

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.

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”

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