Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 36 ยท State Machines and Design Patterns

Event-Driven User Interfaces

Use the LabVIEW Event structure to react to value changes, key presses and mouse actions without polling. Covers event cases, filter events, user events, timeouts and the latched-button trap.

A beginner's user interface polls: a loop reads every button on every iteration and asks "was it pressed?". It wastes CPU, can miss fast clicks and tangles the logic. The Event structure inverts this. It sleeps until something happens and then runs exactly the case for that event. Together with the queued message handler it is the standard way to build responsive LabVIEW interfaces.

LabVIEW itself cannot run on this site, so the editing steps are described in words and a Python stand-in models an event dispatcher.

What an Event structure looks like

The Event structure (Programming > Structures) resembles a Case structure with a timeout terminal on its left border (-1 waits forever) and an event data node inside every case that carries details of the event. You normally put it inside a While Loop. Each iteration: it waits, one event happens, one case runs, the loop checks its stop condition.

To add a case, right-click the border and choose Add Event Case, or Edit Events Handled by This Case, then pick:

  • Event source: a front-panel control (Start), the pane, the VI, the application or a dynamic/user event.
  • Event such as Value Change, Mouse Down, Key Down, Panel Close?, Mouse Enter, or Timeout.

The Timeout case runs when nothing happened for the specified milliseconds. It is a good place for periodic work such as refreshing an indicator.

Notify events and filter events

Events come in two kinds:

Kind Name format What it means
Notify Value Change, Mouse Down Something already happened; LabVIEW reports it.
Filter Panel Close?, Key Down? (with a question mark) Something is about to happen; your code may discard it.

A filter event gives you a chance to veto: in Panel Close? ask "Do you want to save?" and wire True to the Discard? terminal to keep the window open. The event data node shows both the original and the changed value.

Event structures hold the front panel. While a case runs, other events for that structure queue up and the panel can look frozen. Keep each case short and delegate long work by enqueueing a message.

The latched-button trap

A latch boolean (switch when released) resets itself only after the VI reads it. In an Event structure with a Value Change case for that button, the reset happens when the terminal is read inside the case, so wire the control's terminal in the case. If you forget, the next time the Event structure runs the button may fire again or not at all.

Another typical mistake is putting the Event structure in the loop without a Wait and also reading controls with local variables, which brings the polling cost back. Let the structure block.

Dynamic and user events

  • Dynamic events register controls at run time (Register For Events), useful for generated UI.
  • User events let your own code send events: Create User Event (give it a data type), Register For Events (wire the event reference to the Event structure's dynamic terminal), and Generate User Event to fire it. This lets a worker loop notify the UI loop without polling.
  • Event reference lifetimes: unregister with Unregister For Events and destroy with Destroy User Event after the loop ends.

The big advantage over a queue is that a single Event structure can wait on UI events and user events at once.

A dispatcher stand-in

The code below registers handlers for named events and dispatches a list of events, including a filter event that can discard the close request, and a timeout tick. It illustrates the logic of one pass through the structure per event.

Python An event dispatcher (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Responding to keys and the mouse

Useful event data:

  • Key Down gives Key and Modifiers (Ctrl, Shift). Use it for shortcuts; set the control's Key Focus or use the Pane's Key Down event.
  • Mouse Down/Up/Move give coordinates, button and click count; combined with graph references you can build custom cursors.
  • Mouse Enter/Leave drive hover help.
  • Value Change gives the old and new value of a control, so you can detect which element of an array or which button of a cluster changed.

Making it a template

A robust skeleton is:

  1. Before the loop: Obtain Queue, Create User Event, initial UI state.
  2. Event loop with one Event structure: Timeout disabled (-1) unless needed; cases Start, Stop, Panel Close?, <other controls>.
  3. In each case: enqueue a message; set Stop to end the loop.
  4. After the loop: unregister events, release queues.

Handling a front-panel Stop control that the Event structure has registered, rather than one the loop reads directly, prevents the common race where the loop terminates while a case is half done.

Debugging events

  • Event Inspector Window (Tools > Advanced > Event Inspector) lists registered events and their queue lengths.
  • A control with a Value Change case configured for it is shown with an event icon in the Edit Events dialog; use Lock if you want the panel locked while the event runs.
  • If a case seems never to fire, check that Lock front panel until the event case completes is not hiding the issue, and that the Event structure is inside the loop that is actually running.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why prefer an Event structure over polling controls in a loop?

    The structure blocks efficiently and runs the case for each event.
  2. 2. What distinguishes a filter event such as Panel Close? from a notify event?

    Filter events (with a question mark) fire before the action and let you veto it.
  3. 3. How can a worker loop tell an Event structure that data are ready?

    User events are created, registered and generated programmatically.
  4. 4. What happens if an Event case takes 10 seconds to run?

    One case runs at a time; long work should be delegated to another loop.

Summary

  • The Event structure waits for UI, user and dynamic events and runs one case per event.
  • Notify events report; filter events (question mark) can be discarded.
  • Keep cases short and delegate work through queues; use user events to talk back to the UI.
  • Unregister events and release references when the loop ends.

Next: Debugging Tools: Probes and Highlight Execution