Share via


Window events

Windows provide the following events:

  • OpenBeforeOriginal
  • OpenAfterOriginal
  • ActivateBeforeOriginal
  • ActivateAfterOriginal
  • CloseBeforeOriginal
  • CloseAfterOriginal
  • PrintBeforeOriginal
  • PrintAfterOriginal
  • BeforeModalDialog
  • AfterModalDialog

OpenBeforeOriginal

This event occurs when the window is opened, but before the window's open event is run. This event can be canceled.

OpenAfterOriginal

This event occurs when the window is opened, but after the window's open event is run.

ActivateBeforeOriginal

This event occurs each time the window becomes active, before the window's activate event is run. The activate event occurs every time the window is opened or brought to the front by the user. It occurs after the Open event.

Warning: The Activate event must be used cautiously. It shouldn't perform any actions that can cause dialogs to appear because the application can become suspended in an endless loop.

ActivateAfterOriginal

This event occurs each time the window becomes active, after the window's activate event is run.

CloseBeforeOriginal

This event occurs when the window is closed, but before the window's close event is run. This event can be canceled.

CloseAfterOriginal

This event occurs when the window is closed, but after the window's close event is run.

PrintBeforeOriginal

This event occurs when the print action for the window is chosen, but before the window's print event is run. The print action for a window occurs when the user chooses the Print menu item from the File menu, or clicks the Print button on the window. This event can be canceled.

PrintAfterOriginal

This event occurs when the print action for the window is chosen, but after the window's print event is run.

BeforeModalDialog

This event occurs when the a modal dialog is displayed by a window, but before the dialog is actually shown to the user. The event arguments allow access the properties of the modal dialog:

DialogType   Indicates the type of modal dialog that is being displayed. The DialogType enumeration defines the following available types:

Type

Description

Ask

A modal dialog generated by the error, warning, or ask() script commands.

GetString

A modal dialog generated by the getstring() command.

Message   Contains the text that is displayed in the modal dialog. Setting the value of this property in the BeforeModalDialog event allows you to change the text displayed in the modal dialog. Typically, the value of this parameter is examined to find out what message is going to be displayed to the user.

Button1Text   Contains the text that is displayed in button 1 of the modal dialog. Setting the value of this property in the BeforeModalDialog event allows you to change the text displayed in the button.

Button2Text   Contains the text that is displayed in button 2 of the modal dialog. Setting the value of this property in the BeforeModalDialog event allows you to change the text displayed in the button.

Button3Text   Contains the text that is displayed in button 3 of the modal dialog. For getstring() dialogs, this is the editable string in the dialog. Setting the value of this property in the BeforeModalDialog event allows you to change the text displayed in the button or the editable field.

Response   Indicates the response to the modal dialog. The DialogResponse enumeration defines the available responses:

Response type

Description

Button1

Button 1 in the modal dialog is clicked.

Button2

Button 2 in the modal dialog is clicked.

Button3

Button 3 in the modal dialog is clicked.

Ok

The OK button in the getstring() modal dialog is clicked.

Cancel

The Cancel button in the getstring() modal dialog is clicked.

None

No response was made.

Setting this property in the BeforeModalDialog event allows your code to respond to the dialog, and prevents the dialog from being displayed to the user. Setting the response to None will allow the dialog to be displayed.

The following Visual Basic example creates references to the Sales Transction Entry form and window, and then registers a BeforeModalDialog event for the Sales Transaction Entry window.

' Create a reference to the Sales Transtion Entry form
Shared SOPEntryForm As SopEntryForm = Dynamics.Forms.SopEntry

' Create a reference to the Sales Transaction Entry window
Shared SOPEntryWindow As SopEntryForm.SopEntryWindow = SOPEntryForm.SopEntry

Sub Initialize() Implements IDexterityAddin.Initialize

    AddHandler SOPEntryWindow.BeforeModalDialog, AddressOf _
    SopEntryWindow_BeforeModalDialog

End Sub

The following example is the BeforeModalDialog event handler for the event registered inthe previous example. When a modal dialog is to be displayed, the code examines the text of the message to determine whether it is the message that asks the user whether they want to add a new customer or a new prospect. If it is, the Response parameter of the event arguments is set to Button1, indicating that a new customer should be created. This prevents the modal dialog from being displayed.

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

AfterModalDialog

This event occurs when the user has acted on a modal dialog displayed by a window. This event allows you to retrieve the choice that the user made in the modal dialog. Typically, you will examine the DialogType and Message properties in the event arguments to find out what dialog is being displayed. Then you can examine the Response property in the event arguments to learn how the user responded to the dialog.

The following C# example creates references to the Item Maintenance form and the Item Maintenance window, and then registers for the AfterModalDialog event for the Item Maintenance window.

// Create a reference to the Item Maintenance form
public static IvItemMaintenanceForm ItemMaintenanceForm = Dynamics.Forms.IvItemMaintenance;

// Create a reference to the Item Maintenance window
public static IvItemMaintenanceForm.IvItemMaintenanceWindow ItemMaintenanceWindow = ItemMaintenanceForm.IvItemMaintenance;

public void Initialize()
{
    ItemMaintenanceForm.IvItemMaintenance.AfterModalDialog +=
    new EventHandler<AfterModalDialogEventArgs>
    (IvItemMaintenance_AfterModalDialog);
}

The following C# example is the AfterModalDialog event handler for the event registered inthe previous example. When a modal dialog is to be displayed, the code examines the text of the message to determine whether it is the message that asks the user whether they want to delete the current item. The Response property of the event arguments is examined to find out whether the user clicked Delete in the modal dialog. If the user did, the additional information stored for the item is also deleted.

void IvItemMaintenance_AfterModalDialog(object sender, AfterModalDialogEventArgs e)
{
    // Is it an 'ask' dialog?
    if (e.DialogType == DialogType.Ask)
    {
        // It it verifying a delete action?
        if (e.Message.Contains("want to delete"))
        {
            // Did the user click Delete (button 1)?
            if (e.Response == DialogResponse.Button1)
            {
                // Delete the environmental details for the current item.
                DataAccessHelper.DeleteEnvironmentalDetails();
            }
        }
    }
}