13/04/2013

VB.Net code for preventing duplicate entry in a ComboBox? Vb.Net code for beginners

Sometime when we are entering list of items and list of departments in a combobox and when the list which we are entering is longer then there is chances that some of the items we entered in list box may be duplicate or we can say a double entry of same items may be made. So now the question arises how to to prevent such entries at the time of making it. We can prevent such entry with this vb.net code


Private Sub  BtnAddDep_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles BtnAddDep.Click
'Add department text property to ComboBox listing
Dim FBoolean As Boolean = False
 Dim IndInteger As Integer = 0
Do Until FBoolean = True Or IndInteger =
DepComboBox.Items.Count
If DepComboBox.Text.Trim.ToUpper =
DepComboBox.Items(IndInteger).ToString.Trim.ToUpper Then
FBoolean = True
Else
'Add 1 to index value
IndInteger += 1
End If
Loop 'Loop back and try again
'Now decide whether to add item or not
If FBoolean = False And DepComboBox.Text.Trim
<> String.Empty Then
DepComboBox.Items.Add(DepComboBox.Text.Trim)
Else
MessageBox.Show("Duplicate or Invalid Department Name", "Duplicate Data
Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
DepComboBox.Focus()
DepComboBox.SelectAll()
End If
End Sub


No comments:

Post a Comment

What & How