How to Fill a Combo Box with a Unique List of Items

Last reviewed: September 13, 1995
Article ID: Q136640
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0

SUMMARY

To have a combo box display unique values from a field of a database that contains multiple occurrences of each of those values, use the DisplayValue property in combination with the ListIndex property of the combo box.

MORE INFORMATION

Placing a string in the combo box DisplayValue property automatically sets the combo box ListIndex property to zero (0) if no match is found or to the index position of a matching string within the created list. Therefore, you can loop through until the ListIndex property is 0, and then add the new list item by using the AddItem method. By using a SCAN...ENDSCAN looping structure, you can create a completely unique list as the following example code illustrates.

Sample Code

Place the following code in the combo box Init event to create a unique list of the <field name> contents before displaying the combo box:

   * Init Code
   SCAN
      THIS.DISPLAYVALUE = <field name>
      IF THIS.LISTINDEX = 0      && not already in the list
         THIS.ADDITEM(<field name>)
      ENDIF
   ENDSCAN
   THISFORM.REFRESH

If a new item is typed into the combo box, it must be verified as unique before being added to the current list. To force the ListIndex property to change automatically, add the following code to the Valid event procedure of the combo box:

   * Valid Code
   THIS.VALUE = THIS.DISPLAYVALUE
   IF THIS.LISTINDEX = 0      && Not already in the list
      THIS.ADDITEM(THIS.VALUE)
      THIS.DISPLAYVALUE = THIS.VALUE
   ENDIF

The code assigns the DisplayValue property to the Value property for the combo box. Then it checks the value of ListIndex. If ListIndex is 0, the item is added as before. When this comparison is made, the DisplayValue property loses its contents, so the Value property is reassigned to the DisplayValue property in the IF...ENDIF structure.


Additional reference words: 3.00 VFoxWin
KBCategory: kbtool kbcode
KBSubcategory: FxtoolFormdes


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 13, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.