C# Customization: Access to Epicor existing controls

I am in the process of updating our VB.NET customizations to C# (Both attached, below). I am currently working on Issue Material. (Bolds added for easier reading.)

I am having a problem with getting access to an existing EPICOR textbox or whatever.

In VB.NET I’d use the following code.

                Private withEvents txtPart as epiTextBox
                Private withEvents txtRef, txtPart as epiTextBox
                Private withEvents nbrQty as EpiNumericEditor
	            Private withEvents cmbMtl as EpiUltraCombo
	            Private withEvents btnOK,btnCancel as EpiButton 

	            Private Sub IssueMaterialForm_Load(ByVal sender As Object, ByVal args As EventArgs)
		           nbrQty = CType(csm.GetNativeControlReference("4dad869d-7ffa-45c3-9bf2-bdb96edfb83b"), EpiNumericEditor)
		           cmbMtl = CType(csm.GetNativeControlReference("3c29bb2b-cc78-4cab-80c8-f004923fdbae"), EpiUltraCombo)
		           btnOK = CType(csm.GetNativeControlReference("43ce5d32-8e77-4d69-9a8e-6c33bd4da434"), EpiButton)
		           btnCancel = CType(csm.GetNativeControlReference("e3b58973-656f-42a3-8fe5-e362a0aeb57c"), EpiButton)EpiTextBox)
                End sub

I have the following code for C#:

	private EpiNumericEditor nbrQty;
	private EpiUltraCombo cmbMtl;
	private EpiButton btnOK;
	private EpiButton btnCancel;

	private void IssueMaterialForm_Load(object sender, EventArgs args)
	{
			txtToMtlPart = (EpiTextBox)csm.GetNativeControlReference("6e38f847-830d-400f-a3bd-937a4f1aee04");
			txtRef = (EpiTextBox)csm.GetNativeControlReference("0ff0dfbb-1625-406e-9917-88f3cef0fd5e");
			txtPart = (EpiTextBox)csm.GetNativeControlReference("26fb08e8-8d2e-4d2f-88e5-9f9a8797cc2b");
			nbrQty = (EpiNumericEditor)csm.GetNativeControlReference("4dad869d-7ffa-45c3-9bf2-bdb96edfb83b");
			cmbMtl = (EpiUltraCombo)csm.GetNativeControlReference("3c29bb2b-cc78-4cab-80c8-f004923fdbae");
			btnOK = (EpiButton)csm.GetNativeControlReference("43ce5d32-8e77-4d69-9a8e-6c33bd4da434");
			btnCancel = (EpiButton)csm.GetNativeControlReference("e3b58973-656f-42a3-8fe5-e362a0aeb57c");
	}

##Testing code, compiles successfully, but nothing works when I enter or leave the nbrQty.

##Further reading, I’ve found that I need to add the following

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

		this.baseToolbarsManager.ToolClick += new Infragistics.Win.UltraWinToolbars.ToolClickEventHandler(this.baseToolbarsManager_ToolClick);
		this.IM_DataView = this.IM_Row.dataView;
		this.IM_DataView.ListChanged += new ListChangedEventHandler(this.IM_DataView_ListChanged);
		this.edvIM = ((EpiDataView)(this.oTrans.EpiDataViews["IM"]));

		// End Wizard Added Variable Initialization

		// Begin Wizard Added Custom Method Calls
		this.nbrQty.Leave += new System.EventHandler(this.nbrQty_Leave);
		this.txtRef.Enter += new System.EventHandler(this.txtRef_Enter);
		this.txtRef.Leave += new System.EventHandler(this.txtRef_Leave);
		this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
		this.btnCancel.Click += new System.EventHandler(this.btnCancel_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
		this.nbrQty.Leave -= new System.EventHandler(this.nbrQty_Leave);
		this.txtRef.Enter -= new System.EventHandler(this.txtRef_Enter);
		this.txtRef.Leave -= new System.EventHandler(this.txtRef_Leave);
		this.btnOK.Click -= new System.EventHandler(this.btnOK_Click);
		this.btnCancel.Click -= new System.EventHandler(this.btnCancel_Click);

		// End Wizard Added Object Disposal

		// Begin Custom Code Disposal

		// End Custom Code Disposal
	}

I test the code and it compiles successfully, but when I close the form and reopen, I get the following error and can’t continue any further. Anything will be helpful.

Error Detail
============
Exception has been thrown by the target of an invocation.

Stack Trace
===========
 at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
 at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
 at Epicor.Mfg.UI.Customization.CustomScriptMethodInvoker.InvokeScriptMethod(MethodInfo scriptMethod, Object[] parameters)
 at Epicor.Mfg.UI.Customization.CustomScriptMethodInvoker.InvokeInitializeCustomCodeIfExists()
 at Epicor.Mfg.UI.Customization.CustomScriptManager.TryActionShowExceptionBoxOrLogVerificationErrorIfException(Action action, String exceptionBoxTitle)

Inner Exception
===============
Object reference not set to an instance of an object.

Inner Stack Trace
=================
at Script.InitializeCustomCode()

IssueMaterialForm-VB.vb (8.5 KB)
IssueMaterialForm-Csharp.vb (9.2 KB)

Some of those GUIDS may have changed from version to version. The error you are getting means that one or more of the variables you are using hasn’t been properly initialized

I copied the existing GUID as comments below.

	private void IssueMaterialForm_Load(object sender, EventArgs args)
	{
			txtToMtlPart = (EpiTextBox)csm.GetNativeControlReference("6e38f847-830d-400f-a3bd-937a4f1aee04");
            //                                                        6e38f847-830d-400f-a3bd-937a4f1aee04
			txtRef = (EpiTextBox)csm.GetNativeControlReference("0ff0dfbb-1625-406e-9917-88f3cef0fd5e");
            //                                                  0ff0dfbb-1625-406e-9917-88f3cef0fd5e
			txtPart = (EpiTextBox)csm.GetNativeControlReference("26fb08e8-8d2e-4d2f-88e5-9f9a8797cc2b");
            //                                                   26fb08e8-8d2e-4d2f-88e5-9f9a8797cc2b
			nbrQty = (EpiNumericEditor)csm.GetNativeControlReference("4dad869d-7ffa-45c3-9bf2-bdb96edfb83b");
            //                                                        4dad869d-7ffa-45c3-9bf2-bdb96edfb83b
			cmbMtl = (EpiUltraCombo)csm.GetNativeControlReference("3c29bb2b-cc78-4cab-80c8-f004923fdbae");
            //                                                     3c29bb2b-cc78-4cab-80c8-f004923fdbae
			btnOK = (EpiButton)csm.GetNativeControlReference("43ce5d32-8e77-4d69-9a8e-6c33bd4da434");
            //                                                43ce5d32-8e77-4d69-9a8e-6c33bd4da434
			btnCancel = (EpiButton)csm.GetNativeControlReference("e3b58973-656f-42a3-8fe5-e362a0aeb57c");
	        //                                                    e3b58973-656f-42a3-8fe5-e362a0aeb57c
			//lblTemp.Text = String.Empty;
			//lblTemp.Visible = false;
			MachineInfoVisible(false);
			UserInfoVisible(false);
	}

I don’t see anything different, but for testing I then copied the GUID over the existing GUID.

The problem still persists.

I have been looking at documentation (EpicorCustomization_UserGuide_101500.pdf) pg 462 and it states


Control ctrl = csm.GetNativeControlReference(“5483cdef-3049-4705-b597-28ae93b
c7fdf”);

EpiUltraCombo eucSales = (EpiUltraCombo)csm.GetNativeControlReference(“5483cdef-3049-4705-b597-28ae93bc7
fdf”);

It mentions nothing about adding to InitializeCustomCode() and DestroyCustomCode() which is throwing the exceptions.

So, how do I get the following code to work. The MessageBox.Show() are just for testing purposes.

	private void nbrQty_Leave(object Sender, System.EventArgs Args)	
		{
			MessageBox.Show("nbrQty_Leave"); //For Testing - not showing.
			txtRef.Focus();
			IsOKButtonAvailable();
		}

	private void txtRef_Enter(object Sender, System.EventArgs Args)	
		{
			MessageBox.Show("txtRef_Enter"); //For Testing - not showing.
			IsOKButtonAvailable();
		}
	
	private void txtRef_Leave(object Sender, System.EventArgs Args)
	{
		MessageBox.Show("txtRef_Leave"); //For Testing - not showing.
		if (!string.IsNullOrEmpty(lblTemp.Text))
		{
			txtRef.Text = lblTemp.Text;
			if (!string.IsNullOrEmpty(txtRef.Text))
			{
				txtRef.Enabled = false;
			}
			lblTemp.Text = string.Empty;
		}
		IsOKButtonAvailable();
	}

William
You are trying to do too much at once, to debug a large file full of errors is going to be a night mare.
Remove most of the code and start adding it back in slowly until you figure out what’s causing the error.
Then we can more easily help you fix it

Might be a long shot, but make sure the names of the controls you’ve added are correct in the code.

If you do the following:

  1. Insert a new EpiButtonon the form
  2. Rename the button from epiButtonC1 to btnCopyText
  3. Use the event Wizard to create the foundation for a Click event for your button
  4. Save the Customization

The Wizard will create the underlying architecture for your button, but will use “epiButtonC1” instead of your “btnCopyText”

This happens because the button name hasn’t been update before the wizard runs.

OK, simple code: Get a MessageBox.Show() when entering/leaving an EPICOR designed control on Issue Material in C#.

// **************************************************
// Custom code for IssueMaterialForm
// Created: 4/4/2017 1:25:35 PM
// Customization: CheckIfPrevQtyGTReqQty
// Notes: When the Quantity is greater than ReqQuanity,
//        gather info on machine and user for checking overissues.
//        Write info to PartTran.TranReference for use later in a report.
// **************************************************
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Epicor.Mfg.BO;
using Epicor.Mfg.UI;
using Epicor.Mfg.UI.Adapters;
using Epicor.Mfg.UI.Customization;
using Epicor.Mfg.UI.ExtendedProps;
using Epicor.Mfg.UI.FormFunctions;
using Epicor.Mfg.UI.FrameWork;
using Epicor.Mfg.UI.Searches;
//using Microsoft.VisualBasic;

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 **
	private EpiNumericEditor nbrQty;

	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

		this.nbrQty.Leave += new System.EventHandler(this.nbrQty_Leave); //If this is commented out, no error at load, but still doesn't work.
		this.nbrQty.Enter += new System.EventHandler(this.nbrQty_Enter); //If this is commented out, no error at load, but still doesn't work.
		// 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
		
		this.nbrQty.Leave -= new System.EventHandler(this.nbrQty_Leave);  //If this is commented out, no error at load, but still doesn't work.
		this.nbrQty.Enter -= new System.EventHandler(this.nbrQty_Enter);  //If this is commented out, no error at load, but still doesn't work.
		// End Wizard Added Object Disposal

		// Begin Custom Code Disposal

		// End Custom Code Disposal
	}


	//private void nbrQty_Leave(object Sender, System.EventArgs Args)	 as per ckrusen post - the diffence is Case-sensitivity (corrected)
	private void nbrQty_Leave(object sender, System.EventArgs args)	
	{
		MessageBox.Show("nbrQty_Leave"); //For Testing - not showing.
	}

	private void nbrQty_Enter(object sender, System.EventArgs args)
	{
		MessageBox.Show("nbrQty_Enter"); //For Testing - not showing.
	}

	private void IssueMaterialForm_Load(object sender, EventArgs args)
	{
		nbrQty = (EpiNumericEditor)csm.GetNativeControlReference("4dad869d-7ffa-45c3-9bf2-bdb96edfb83b");
	}
}

Still get error with this “Hello World” message.
It compiles successfully, but get error during the load of form.
Error: Exception has been thrown by the target of an invocation.

Please advise

William,

Throw nbrQty = (EpiNumericEditor)csm.GetNativeControlReference(“4dad869d-7ffa-45c3-9bf2-bdb96edfb83b”);
into your initialization instead of form load.

It’s been a group effort, but Duckor you’ve won the chicken dinner. I have message boxes.

Now, I’ll try with my original code.

Just to clarify, initialization happens before the form load. So before the control was referenced it was trying to setup the event handler.

Now, where do I pick up this chicken? :yum:

1 Like

3 Likes

@duckor
Makes sense, now.

I am coming from programming vb.Net to C# and the Form_Load has been where I’ve put the controls.
The chickens in the mail.

Props to @ckrusen
When I went back to my origcode, I still had errors in the contents in the () had to be changed also.

  //private void nbrQty_Leave(object Sender, System.EventArgs Args)	 as per ckrusen post - the diffence is Case-sensitivity (corrected)
	private void nbrQty_Leave(object sender, System.EventArgs args)	
	{
		MessageBox.Show("nbrQty_Leave"); //For Testing - not showing.
	}

@josecgomez
As always, thanks for your promptness and how to simplify the problem. Love the chicken pic.

1 Like