次の方法で共有


Responding to events

The event handler method contains the code that runs in response to the event. This code can perform whatever action is required for your integrating application. For example, the following C# code is the event handler that runs in response to the value of the Sales Document Number field being changed in the Sales Transaction Entry window. If the EstimateFreight form is open, the controls on the form are set.

void SopNumber_Change(object sender, EventArgs e)
{
    // If the Estimate Freight form is open, update the document number
    // and clear the other controls so the new calculation can be performed.
    if (EstimateFreightForm.Created == true)
    {
        EstimateFreightForm.textBoxDocumentNumber.Text =
        SOPEntryWindow.SopNumber.Value;
        EstimateFreightForm.textBoxEstimatedFreight.Clear();
        EstimateFreightForm.textBoxTotalWeight.Clear();
    }
}

Event arguments

The event arguments for some events provide additional information about the event. The event arguments for procedure events and function events allow access to the parameters for the procedure or function call. For instance, the following C# example is the event handler for InvokeAfterOriginal event for the GetAutoComplete function in Microsoft Dynamics GP. The event arguments contain the parameters that were passed into the function. The event handler examines the value of the first parameter to verify that its value is "DYNSA". If it is, the code sets the result of the function to false.

void GetAutoComplete_InvokeAfterOriginal(object sender, SyUserDefaultsForm.GetAutoCompleteFunction.InvokeEventArgs e)
{
    // Set the return value to always turn off auto-complete for DYNSA user
    if (e.inParam1 == "DYNSA")
    {
        e.result = false;
    }
}

The event arguments for modal dialog events allow access to the buttons and message displayed in the modal dialog. For example, the following Visual Basic code is the event handler for the BeforeModalDialog event of the Sales Transaction Entry window. It shows how the event arguments are used to examine the message text displayed by the modal dialog, and then used to respond to the dialog.

Private Sub SopEntryWindow_BeforeModalDialog(ByVal sender As Object, ByVal e As BeforeModalDialogEventArgs)

    ' Examine the dialog to determine whether it is the one to respond do
    If e.DialogType = DialogType.Ask Then
        If e.Message.Contains("want to add a customer") Then
            ' Click the Customer button in the modal dialog
            e.Response = DialogResponse.Button1
        End If
    End If
End Sub

Cancelling events

Some events can be cancelled through the event handler. Typically, these are events that occur before the event in the core application, such as the OpenBeforeOriginal event for a form. The event argument parameter that is passed to the event handler for these events has a special type that allows the event to be cancelled.

For example, the following Visual Basic example is the event handler for the BeforeOpen event of the Customer Maintenance form. The event handler method examines the current user logged into the system. If it's the "sa" user, a message is displayed and the window isn't opened. Notice how the event arguments are used to cancel the event.

Public Sub RMCustMaintBeforeOpen(ByVal sender As Object, ByVal e _
As System.ComponentModel.CancelEventArgs)

    If Dynamics.Globals.UserId.Value = "sa" Then

        'Display the message
        MsgBox("Do not access this window as the Administrator")

        'Prevent the form from opening
        Dynamics.Forms.RmCustomerMaintenance.Close()

        'Cancel pending events
        e.Cancel = True

    End If
End Sub