From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

LabVIEW Button With Drop Down Menu

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

  • LabVIEW

Code and Documents

Attachment

Description

This example demonstrates how to create a custom button that has a drop-down menu, allowing the user to initiate different actions based on the active selection. This is an alternative approach to designing a custom XControl. For demonstration purposes, the button shown performs different stop actions including: stopping the VI immediately, stopping the VI after a pop-up message, and stopping the VI after a user confirmation.

Download the attached ZIP file to see how a Ring Control combined with a Boolean Control is used to achieve this functionality.
 
Prerequisites

How to Use

To build this code from scratch, follow the steps below:

  1.  Launch LabVIEW and create a new project by clicking File >> Create Project.
  2.  Save the project as "Button With Drop Down Menu.lvproj".
  3. From the project window, create a new VI by right-clicking My Computer and selecting New >> VI.
  4. Save the VI as "Button With Drop Down Menu.vi".
  5. Create a new Type Definition by right-clicking My Computer and selecting New >> Type Definition. This will be used later in the VI.
  6. Save the Type Definition as "Case Select.ctl". The project hierarchy should now resemble the following picture.
    1. project hierarchy.PNG
  7. Open "Button With Drop Down Menu.vi" and implement a State Machine architecture.
    • On the Block Diagram, place a While Loop from the Structures palette.
    • Inside the While Loop, place a Case Structure (found in the same palette).
    • Inside the Case Structure, place an Event Structure.
    • State Machine Architecture.png
    • Note: this image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
    •  
  8. Design the button.
    • On the Front Panel, place a Menu Ring from the Ring & Enum palette.
    • Right-click the Menu Ring and select Edit Items...
    • In the pop-up window, add the items shown below.
    • Ring Properties.PNG
    • Place an OK Button from the Boolean palette onto the Front Panel. Change the text to display "Stop Immediately".
    • Move and resize the button to sit on top of the Menu Ring.
    • Right-click on the button and navigate to Mechanical Action. Ensure that this is set to Latch When Released.
    • Front Panel Buttons.PNG
  9. Configure the Case Select Type Definition.
    • Open the "Case Select.ctl" file.
    • From the Ring & Enum palette, place an Enum.
    • Right-click the Enum and select Edit Items...
    • In the pop-up window, enter the items shown below. Click OK and then save the file.
    • Enum Properties.PNG
    •  
  10. Create an initialization case.
    • Drag and drop the "Case Select.ctl" from the LabVIEW project onto the Block Diagram of "Button With Drop Down Menu.vi".
    • Make the "Initialize" case active.
    • Wire the Enum constant to the Conditional Terminal of the Case Structure.
    • Replace the While Loop terminals with Shift Registers by right-clicking on the left-hand terminal and selecting Replace with Shift Register.
    • Right-click the drop-down menu on the Case Structure and select Add Case for Every Value. Make the active case "Initialize".
    • Place a copy of the Case Select Type Definition inside the "Initialize" case. Set the active case to "Wait" and connect it to the right-hand Shift Register of the While Loop.
    • Right-click the Ring Control and Boolean Control and select Create >> Reference for each. Place the references inside the "Initialize" case.
    • From the Application Control palette, place an Invoke Node. Connect the Ring Control reference to the reference input. Repeat this for the Boolean Control.
    • Set the method for each Invoke Node to Reinitialize to Default.
    • From the Application Control palette, place a Property Node. Connect the reference out of the Ring Control Invoke Node to the reference in of the Property Node.
    • Set the property to Ring Text >> Text.
    • Place another Property Node and connect it to the reference out of the Boolean Invoke Node to the reference input.
    • Set the property to Boolean Text >> Text. Right-click the property and select Change to Write.
    • Connect the RingText.Text output to the BoolText.Text input.
    • Connect the error wires of the Invoke and Property Nodes and wire them to the While Loop. Replace the While Loop terminals with Shift Registers.
    • Place a False Constant from the Boolean palette inside the Case Structure. Connect this to the stop condition terminal of the While Loop.
    • Right-click the boolean terminal and select Use Default if Unwired.
    • Initialize case.png
    • Note: this image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
  11. Create a wait case.
    • Navigate to the case that includes the Event Structure.
    • Right-click the drop-down menu on the Case Structure and click Swap Diagram With Case >> "Wait".
    • Right-click the Event Structure and select Add Event Case.
    • In the Edit Events window, select the Ring Control in the Event Sources tree. Ensure that Value Change is selected under Events. Click OK.
    • Place the Ring Control inside the Event Case.
    • Place a copy of the Case Select Type Definition inside the Event Case. Make the active selection "Change Stop Text". Connect this to the corresponding While Loop Shift Register.
    • Connect the error wire input to the error wire output on the Case Structure.
    • Ring Event Case.png
    • Note: this image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
    • Add another Event Case for the Boolean Control. This should also be a Value Change event.
    • Place the Boolean Control inside the Boolean Event Case.
    • Place a Case Structure from the Structures palette inside the Event Case.
    • Right-click the Boolean Control and select Create >> Property Node >> Boolean Text >> Text.
    • Wire the BoolText.Text output to the conditional terminal of the Case Structure. Connect the error wire to the Property Node.
    • Rename the "True" case of the Case Structure to "Stop Immediately".
    • Copy the Case Select Type Definition into the "Stop Immediately" case and set the active selection to "Stop Immediately". Connect this to the corresponding Enum terminal on the Event Structure.
    • Repeat the last 2 steps for cases: "Display Message Stop" and "Confirm Stop". The case name should match with the active Enum selection.
    • Navigate to the "False" case. Right-click the Case Structure and select Delete This Case.
    • Navigate to the "Stop Immediately" case. Right-click the Case Structure and select Make This The Default Case.
    • Boolean Event Case.png
    • Note: this image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
  12. Define the "Change Stop Text" case.
    • From the "Wait" case, right-click the Ring Control and select Create >> Property Node >> Ring Text >> Text
    • Repeat the last step for the Boolean Control and select the property Boolean Text >> Text.
    • Navigate to the "Change Stop Text" case and place both Property Nodes inside.
    • Right-click the BoolText.Text and select Change To Write.
    • Connect the output of RingText.Text to the BoolText.Text input. Connect the error wire through the Property Nodes.
    • Place a copy of the Case Select Type Definition inside the Case Structure, and make the active selection "Wait".
    • Change Stop Text Case.png
    • Note: this image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
  13. Define the "Stop Immediately" case.
    • Navigate to the "Stop Immediately" case.
    • Connect the Select Case Type Definition wire through the Case Structure. Repeat this with the error wire.
    • From the Boolean palette, place a True Constant.
    • Connect the True Constant to the stop condition of the While Loop.
    • Stop Immediately Case.png
    • Note: this image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
  14. Define the "Display Message Stop" case.
    • Navigate to the "Display Message Stop" case.
    • Wire the Select Case Type Definition and error wires through the Case Structure.
    • From the Dialog & User Interface palette, place a One Button Dialog.
    • Right-click the message input and select Create Constant.
    • In the string constant, enter the following text: "This VI is about to stop."
    • Connect the true output of the One Button Dialog to the stop condition of the While Loop.
    • Display Message Stop Case.png
    • Note: this image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
  15. Define the "Confirm Stop" case.
    • Navigate to the "Confirm Stop" case.
    • Connect the error wire through the Case Structure.
    • From the Dialog & User Interface palette, place a Two Button Dialog.
    • Right-click the message input and select Create Constant.
    • In the string constant, type: "Do you want to stop this VI?"
    • Connect the true output to the stop condition of the While Loop.
    • Connect a copy of the Select Case Type Definition to the corresponding output terminal. Make the active case "Wait".
    • Confirm Stop Case.png
    • Note: this image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
  16. Run the VI and observe the different actions that take place depending on the Ring Control selection.

 

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