Sign In
HOWTO: Prevent duplicate items in a ComboBox 


When populating a ComboBox, it is necessary to verify that duplicates are not being added to the ComboBox.  One way to do this is by using the Contains() method.  When using this method, precautions must be taken to ensure that it works as we want it to.


The list of items in a ComboBox is stored in its Items property.  Items is an ObjectCollection which allows access to several useful methods and properties.  The Add() method, for example, is used to add an item to a ComboBox:

combobox1.Items.Add( "A. Customer" )

combobox1.Items.Add( myString )

Another useful method is Contains(), which determines if a particular item is present in the ComboBox:

combobox1.Items.Contains( "B. Client" )

If the exact string "B. Client" is already present in the ComboBox, the expression above evaluates to *TRUE.  This makes it very easy to test for duplicates before adding a new item to the ComboBox.  When using this method, however, it is important to take into account the entry of bad data, as well as the datatype that is being used to populate the ComboBox.

For example, when populating a ComboBox with data coming from a fixed-length field, the extra spaces added to the end of fields that do not take up the entire field length have to be accounted for.  If we were to read the string "Bob'sCompany" from a 15-character length field, three spaces would be added to the end in order to make it 15 characters long.  When this value is read from the database and then placed into a ComboBox the three extra spaces are also included.  While on the surface, this should not appear to be important, the extra spaces will cause the Contains method to not work as expected.

The Contains() method does a character-by-character comparison, thus the two fields "Client" and "Client  " are not considered to be equal to each other.  This also applies to leading spaces as well: the string "  Client" is also not considered to be equal to "Client". 

The solution is to use the %TRIM function to remove leading and trailing spaces from a string before adding it to the ComboBox.  The following line from the example project (line 132) uses the %TRIM function to remove extra spaces from the customer name before it is added to the ComboBox:

combobox1.Items.Add( %Trim( CMName ) )

%TRIM should also be used before using the Compare() method to search the ComboBox for duplicates (the following are lines 144-146 from the example)

CustName = %Trim( textbox1.Text )

If ( ( combobox1.Items.Contains( CustName ) = *TRUE ) )


This AVR .NET 8.1 example project contains a single Windows form with a ComboBox that is populated using values from the CMastNewL1 file of the Examples database that is installed with AVR.  There is also a textbox where a new value to be added to the ComboBox can be entered, and a button that when pressed, will attempt to add the value in the textbox to the ComboBox.  If you attempt to add an identical item, you will see an error message

While this method is not quite completely bulletproof (it can be fooled by adding extra spaces in the middle of a string) it works well for most applications.

Related Articles
Article Downloads
combobox, AVR.NET development
Article ID: 463 
Category: ASNA Visual RPG; ASNA Visual RPG : Windows Development 
Applies To: ASNA Visual RPG .NET 
Article Date: 5/14/2008