Recording macros

In your Visual Studio Tools form, identify the places that the user can perform actions. Actions include things like clicking buttons, adding text to text fields, or moving the focus. In the events that run due to these user actions, you will add code that calls the RecordMacroItem() method to record a text string and optional comment that identify and describe the action. For example, the following C# code records a macro command for the Energy Star check box:

RecordMacroItem("ClickHit EnergyStar", "Toggle the Energy Star check box");

The text strings passed to the RecordMacroItem() method are written to the macro as ShellCommand macro statements.

ShellCommand 'ClickHit EnergyStar' # Toggle the Energy Star check box

Hint: The RecordMacroItem() method records values only when a macro is being recorded.

Macro syntax

The text string and optional comment recorded in the macro are the only information available to your Visual Studio Tools add-in when the macro is played back. These text strings must contain enough information for your code to determine the action that must be performed.

There is no predefined syntax for the text strings recorded. You must define the text strings so that they can be parsed by your add-in, and then the add-in can perform the actions indicated. The macro support in the Environmental Details Visual Studio Tools sample is a good starting point. The syntax of its macro statements is similar to the standard Microsoft Dynamics GP macro language.

Placement

Where you place the RecordMacroItem() calls in your code can affect how data is written to the macro. Some experimenting may be required to find which event for a control is most appropriate for recording a macro action. For example, when recording the text entered into a text box control, the Validating event is the most appropriate place for the RecordMacroItem() call. The following C# example shows the Validating event for the YearlyEnergyCost text box in the Environmental Details sample.

private void textBoxYearlyEnergyCost_Validating(object sender, CancelEventArgs e)
{
    // Set the change flag for the Item Maintenance window
    GPAddIn.ItemMaintenanceWindow.IsChanged = true;

    // If a macro is being recorded, record the value.
    RecordMacroItem("TypeTo YearlyEnergyCost \"" +
    textBoxYearlyEnergyCost.Text + "\"", String.Empty);
}

The following table lists the event where the macro recording code should be placed for common controls.

Control

Event

Text box

Validating

Push Button

Click

Check box

Validating