Important alert: (current site time 10/24/2014 4:56:02 PM EDT)
 

VB icon

Checking ComboBox's Items for duplicates

Email
Submitted on: 5/12/2003 2:47:27 PM
By: Conrad B. 
Level: Beginner
User Rating: By 1 Users
Compatibility: VB.NET
Views: 8917
 
     This sample is useful for beginners. It demonstrates how to check comboBox's items for duplicates. The problem is basic and simple. A dropdown combobox contains a list of names, but the program must kick out duplicates. Unfortunately, very often programmers handle this issue with very complicate and inefficient code. The intention of this program is to show that VB.NET allows for very simple and efficient approach to the problem.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Checking ComboBox's Items for duplicates
// Description:This sample is useful for beginners. It demonstrates how to check
comboBox's items for duplicates. The problem is basic and 
simple. A dropdown combobox contains a list of names,
 but the program must kick out duplicates. Unfortunately, 
very often programmers handle this issue with
very complicate and inefficient code. 
The intention of this program is to show that VB.NET allows for very simple and efficient approach to the problem.
// By: Conrad B.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1214&lngWId=10//for details.//**************************************

'--------------------------------------------------------------------
' / This sample demonstrates how to check
' / comboBox's items for duplicates using the FindStringExact function. 
' / 
' / Note: You can copy and paste the content of this snippet into a file with 
' / extension .vb, then compile the file using vb compiler: vbc.exe.
' / When writing a command for compiling the sample, remember that each 
' / assembly whose types are used within the sample must be referenced using
' / a separate /r parameter.
'--------------------------------------------------------------------
' enforce explicite narrowing conversion
Option Strict On
Imports System
Imports System.Windows.Forms
Namespace SampleSourceCode
Public Class ComboDuplicates
		
	Inherits System.Windows.Forms.Form
	
	
	Private butAdd As Button
	Private testCombo As ComboBox
	Private label1 As Label
	Private errorMessage As ErrorProvider
	
		 
	 Public Sub New() 'construct
	 MyBase.New()
	 Me.InitializeComponent()
	 	Me.errorMessage = New ErrorProvider()
	 End Sub
 	<System.STAThreadAttribute()> Public Shared Sub Main() ' entry point
	System.Windows.Forms.Application.Run(New ComboDuplicates())
 End Sub
	
	Private Sub InitializeComponent()
		
		' create the controls
		Me.butAdd = New Button()
		Me.testCombo = New ComboBox()
		Me.label1 = New Label()
		
				
		' configure Button control
		Me.butAdd.Location = New System.Drawing.Point(32,88 )
		Me.butAdd.Size = New System.Drawing.Size(140, 24)
		Me.butAdd.TabIndex = 2
		Me.butAdd.Text = "Add"
	 AddHandler Me.butAdd.Click, AddressOf Me.butAdd_AddItemToCombo
		
		' configure ComboBox control
		Me.testCombo.Location = New System.Drawing.Point(8, 48)
	Me.testCombo.Size = New System.Drawing.Size(180, 21)
	Me.testCombo.DropDownWidth = 180
	Me.testCombo.Items.AddRange(New Object() {"Lolo", "Bolo", "Tara", "John", "Mark"})
	Me.testCombo.TabIndex = 1
		
		' configure Label control
		Me.label1.Location = New System.Drawing.Point(8, 224)
		Me.label1.Size = New System.Drawing.Size(0, 25)
		Me.label1.TabIndex = 0
		Me.label1.Text = "Test"
 		Me.label1.Dock = DockStyle.Bottom
 		label1.BackColor = System.Drawing.Color.LightSteelBlue
 		' add the controls to the form
 		Me.Controls.AddRange(New Control() {Me.testCombo,Me.butAdd, Me.label1})
 		
 		' configure the form
 		Me.Text = "ComboBox Duplicates"
		Me.FormBorderStyle =FormBorderStyle.FixedSingle
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		Me.ClientSize = New System.Drawing.Size(250, 160)
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		
	End Sub
	
	Private Sub butAdd_AddItemToCombo(ByVal sender As Object, ByVal e As System.EventArgs)
	 	
	 	' if Text is empty, exit the sub
	 	If testCombo.Text.Equals("") then
			displayMessage("Empty Field!")
			return
		End If
	 	
	 	' search for a match;
	 	' this function is not case sensitive
	 	Dim index As Integer = _
		testCombo.FindStringExact(testCombo.Text())
	
	' evaluate the result;
	' take appropriate action
	 If index >= 0 Then
		testCombo.Text = "" 
		 	 displayMessage("Duplicate")
		 Else
		testCombo.Items.Add(testCombo.Text)
		displayMessage("")
 	 End If
 	 
	End Sub
	
	Private Sub displayMessage(ByVal message as String)
		
		' display error messages
		
		errorMessage.SetError(testCombo, message)
		
		If message.Equals("Duplicate") Then
			label1.Text = "Can't add a duplicate"
		
		Else If message.Equals("Empty Field!")
			label1.Text = "Enter a name into the text field. "
		Else
			label1.Text = "Name: " & testCombo.Text() & _
			" has been added to the collection of items." 
		End If
	
	End Sub
End Class	
End Namespace


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.