Close form after Clicking OK button

We use the MES station to report production and print a label. Upon entering the transaction and clicking OK we have found our users like to leave the form open to use again which causes the label to not print the next time. We have found by closing the screen after each transaction the label prints perfectly every time. So to keep it simple lets just use the Return Material form. After the user clicks OK and the transactions process how can I get it to close automatically. I have played around and added a custom button and then created a click event and got the custom button to close the form. Below is the code in that event. Not sure how to do it when the user clicks the OK button already there.
{
EpiBaseForm frm = (EpiBaseForm)(csm.GetNativeControlReference(“2dcd1674-5e34-4d98-b493-c75747027376”));
frm.Close();
}

You can add events to native controls.

I tried the suggestion but does not seem to work. Below is the code. Any Suggestions?

// **************************************************
// Custom code for ReturnMtlForm
// Created: 2/1/2020 2:47:50 PM
// **************************************************
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Erp.Adapters;
using Erp.UI;
using Ice.Lib;
using Ice.Adapters;
using Ice.Lib.Customization;
using Ice.Lib.ExtendedProps;
using Ice.Lib.Framework;
using Ice.Lib.Searches;
using Ice.UI.FormFunctions;

public class Script
{
// ** Wizard Insert Location - Do Not Remove ‘Begin/End Wizard Added Module Level Variables’ Comments! **
// Begin Wizard Added Module Level Variables **

// End Wizard Added Module Level Variables **

// Add Custom Module Level Variables Here **
public EpiButton myClose;

public void InitializeCustomCode()
{
	// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Variable Initialization' lines **
	// Begin Wizard Added Variable Initialization

	// End Wizard Added Variable Initialization

	// Begin Wizard Added Custom Method Calls

	myClose = (EpiButton)(csm.GetNativeControlReference("3115995e-cde2-4873-8fd2-ee8b38187dda"));
	this.myClose.Click += new System.EventHandler(this.myClose_Click);

	// End Wizard Added Custom Method Calls
}

public void DestroyCustomCode()
{
	// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
	// Begin Wizard Added Object Disposal

	// End Wizard Added Object Disposal

	// Begin Custom Code Disposal

	this.myClose.Click -= new System.EventHandler(this.myClose_Click);		

	// End Custom Code Disposal
}

private void myClose_Click(object sender, System.EventArgs args)
{
EpiBaseForm frm = (EpiBaseForm)(csm.GetNativeControlReference("2dcd1674-5e34-4d98-b493-c75747027376"));
frm.Close();
}

}

Does it not compile? Or just not close the window?

Throw a message box in myClose_Click function to make sure its being called.

It does compile but does not close the window. Working to put a messagebox in there to test further.

Tried it this morning with a message box and the message box shows then the screen closes. Too the message box out and now it works. Confused. Maybe it takes a while before it works?

I found that the changes to a customization don’t always affect the window that you just customized. I always do the following:

  1. Enable Developer mode
  2. Launch the program to customize
  3. Select the customization in the “Select customization” window
  4. After the form loads, enter customization mode
  5. Do the customizations, save, and exit customization mode
  6. CLOSE THE WINDOW I JUST CUSTOMIZED
  7. Do 2 & 3 again, then test customization (not in customization mode)