Example Code

Combo Box Creates "Search Engine"-Like Search-History and Auto-Complete in LabVIEW

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

If you are looking to create a "search engine"-type control that can access a list of previous data entries and use auto-complete for searches, use a Combo Box. A Combo Box can store a list of previously entered strings, similar to a search history. The attached example can serve as a base to start your own combo box for your project.

 

Note: Code attached is compatible with LabVIEW 2016 and later.

 

How to Use

In the attached example, the Combo Box string control serves as the "search engine" input and each "query" or input is saved in an array which is the "search history."

 

  1. When you open the VI, you will note that the Combo Box drop-down has is prepopulated with some items. If you want to edit or add items to the Combo Box drop-down, you can right-click on the Combo Box >> Edit Items ... >> Insert initial string values.
  2. Run the VI. 
    1. Note that the Stored Strings array on the right side of the front panel populates with the Combo Box values. 
    2. The Sored Strings array is not necessary for the functionality of this example, just serves to visualize the previous searches.
  3. Input a string (e.g. "what's up?") into the Combo Box control and press Enter. In the example, pressing enter adds an element into the Stored Strings array and updates the array stored in the Combo Box Strings[] property.
    • Adding the array to the Combo Box Strings[] Property is what allows the object to show up in the drop-down for the control, and therefore allows the auto-complete nature of the control.
  4. Now type another string in the Combo Box control that starts with the same letter/term as your previous search (e.g. "what's").
    • Note that the Combo Box will present suggestions from your previous searches (e.g. " up?" is highlighted) and if you press Enter at this time, your search will auto-complete with the suggestion.
    • Note that if you deviate from your previous search (e.g. "what's for dinner?"), the auto-complete suggestion leaves upon the first character deviating from the previous search. 
  5. Now add more searches and press Enter each time. Watch the Combo Box drop-down and Stored Strings array fill up with your inputs.
    1. This example code limits the number of saved entries to the last 10 strings. This is defined in the "False" case within the "Enter Button" event. When there are fewer than 10 saved entries, a new entry can simply be inserted into the next index of the array. 
  6. Once you reach search #11, the Stored Strings array will add an element to capture your recent input and delete the oldest element. This way, there is always a list of <10 elements. 
    • If you want to adjust the array size to be n items:
      1. Change the input to the Compare function inside the Enter Button Event Structure to value n - 1 (because arrays are 0-indexed)
      2. Change the index from the Delete Array function in the False Case of the event's Case Structure to n.
  7. The VI can be stopped by hitting the Stop button on the Front Panel. 
    • In current state, stopping the VI will not clear the array or Combo Box strings, but the code could be edited to do so if desired.

 

 

 

 

Related Links

 

 

 

 

Erika
Technical Support Engineer
National Instruments

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

Contributors