Registering events

Your integrating application must register every event that it should be notified of in the system. Event registrations are added to the Initialize() method, located in the template code that is automatically added when you created your Visual Studio Tools for Microsoft Dynamics GP project.

The resources for which you can register events, such as forms, windows, or fields, each list the events they make available. For example, a form has the following events available:

  • OpenBeforeOriginal
  • OpenAfterOriginal
  • CloseBeforeOriginal
  • CloseAfterOriginal

Refer to each resource type described in Resource Reference, for the list of events that are available for that resource.

To register an event, find the resource for which you want to register the event. Pick the event you want, and then specify the event handler method. The event handler is the method that contains the code that runs in response to the event.

C#

Visual Studio will assist you when you register an event in C# code. Begin by referencing the resource for which you want to register an event. Add the += operator, and Visual Studio will display IntelliSense indicating it will complete the event handler. Simply press the tabTAB key to add the event handler registration.

Cc543570.VSTDGP_AddingCSharpEvent(en-us,MSDN.10).gif

After the event handler registration is added, Visual Studio will offer to add the event handler method for the new event. Press the tabTAB key again to add the event handler method. When you're finished, the completed event will look similar to the following:

public void Initialize()
{
    Dynamics.Forms.RmCustomerMaintenance.OpenAfterOriginal += new
    EventHandler(RmCustomerMaintenance_OpenAfterOriginal);
}

void RmCustomerMaintenance_OpenAfterOriginal(object sender, EventArgs e)
{
    throw new Exception("The method or operation is not implemented.");
}

Visual Basic

Registering an event in a Visual Basic project requires two steps. First, in the Initialize() method of the project, the AddHandler statement registers the event. The AddHandler statement takes two parameters. The first specifies the resource and event, while the second specifies the event handler method that will be run. The following example registers the AfterOpen event for the Customer Maintenance form in Microsoft Dynamics GP.

Sub Initialize() Implements IDexterityAddIn.Initialize

    AddHandler Dynamics.Forms.RmCustomerMaintenance.OpenAfterOriginal, _
    AddressOf RMCustMaintAfterOpen

End Sub

In the second step, the event handler method is added. The parameters for this method must match those required by the event. The IntelliSense for the event displays the parameters the event handler must have.

Cc543570.VSTDGP_VBEventHandlerSig(en-us,MSDN.10).gif

The following example shows the event handler method added for the event created in the previous example.

Public Sub RMCustMaintAfterOpen(ByVal sender As Object, ByVal e _
As System.EventArgs)

    MsgBox("Not Implemented")

End Sub