Example Code

Adding a Custom Context Menu to a TestStand User Interface

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • Teststand

Code and Documents

Attachment

Description

Overview

This example shows how you can create a custom context menu in a LabVIEW Built TestStand User Interface.  Unlike an application menu, a context menu is shown when right-clicking a UI pane and contains options relevant to the active pane. This example demonstrates creating a custom context menu when the user right clicks on a step in the sequence pane in the Full Featured UI example which ships with TestStand.

 

Before using this example, you will need to understand the following concepts of TestStand UI development:

If you are not familiar with these topics, review the following documents before using this example

 

Description

 

To add a custom context menu to the TestStand Full Featured UI, this example implements the following changes:

  1. Modify the event callback VI defined for the CreateContextMenu event for the desired control to define the menu items that should appear when the user right-clicks the control. 
    • Create a new custom command object for each new menu item.
    • Add the new commands into a new Commands collection.
    • Insert the commands into the control menu.
  2. To handle custom commands, add the menu item functionality to the PostCommandExecute Event Callback VI.  This VI is executed when the user selects an item on the menu.
    • Use input data to determine what menu item the user selected.
    • Implement the command functionality.

 

Defining the Context Menu

 

Context menus in the TestStand UIs are generated in response to the CreateContextMenu event, which occurs when the user right clicks a TestStand UI control which supports context menus.  Callback VIs, which execute when an event occurs, are defined in the Configure Event Callbacks.vi in the full-featured UI.  In this example, we modify the SequenceView CreateContextMenu Event Callback.vi which defines the context menu that is generated when the user right clicks a sequence view control.

 

eventCallback.png

 

To create a context menu, the following TestStand UI API calls are required:

 

  • ApplicationManager.NewCommands - generate a new commands object, which will contain the commands to display to the user
  • Commands.InsertKind - add a command of a particular type to the list of commands, based on the CommandKind option selected. Some options are a set which contains multiple commands, such as the default value of DefaultSequenceViewContextMenu_Set
  • Commands.InsertIntoWin32Menu - sets the list of commands as the context menu

 

In this example, we replace the DefaultSequenceViewContextMenu_Set with different commands to change the context menu options:

 

updatedCallback.PNG

In this example, two built-in commands (CommandKind_RunModeSkip and CommandKind_RunModeNormal, which set the selected step run mode to skip and normal, respectively) and a custom command are added to the context menu.  To add a custom command, we use the CommandKind_Custom flag and set the userData to a unique value so that the selection can be identified when the user selects an option (details on this are in the next section).  The name of the command in the menu is set using the Command.SetDisplayName method. In this example, the custom command is named "Set Limits", and allows the user to configure the limits for a test step directly.  

 

Implementing the Custom Menu Item Functionality

Now that the menu has been modified, we need to add code to implement the custom command we created.  The built-in commands CommandKind_RunModeSkip and CommandKind_RunModeNormal do not need any additional code since these are handled directly by the SequenceView Manager control.  However, custom commands have no built-in functionality; they only trigger the PostCommandExecute event, where you can add code which executes when the menu item is selected.

 

The PostCommandExecute event is executed for any command the user selects, so it is necessary to check the UserData for the command to check that the menu item which triggered the event is indeed the custom command menu item (Set Limits):

 

executionCallback.PNG

 

Hardware and Software Requirements

 

Custom Context Menu - TS2016.zip

LabVIEW 2013 or Compatible

TestStand 2016 or Compatible

 

Steps to Implement or Execute Code

To use the example:

  1. Download and extract the example files
  2. Open the Build Script.lvproject
  3. Within the project, open the Top-Level VI.vi
  4. Run the VI.  Once started, create a Numeric Limit Test step
  5. Right-click the step.  Observe that the menu contains 3 new items. 

    newMenuItems.png

  6. Click the "Set Limits" item. Observe that the set limits dialog is shown.
Al B.
Staff Software Engineer - TestStand
CTA/CLD

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.