Check for existing value in Combobox list

D

DesCF

I have a textbox and a combobox on a toolstrip. The user enters either an
ID in the textbox or selects a name from the combobox. When the user
selects a name from the combobox the textbox is filled in automatically by
setting its .Text property equal to the .SelectedValue of the combobox.
When the user enters an ID in the textbox the combobox's .SelectedValue is
set to the .Text value in the ID textbox. All works fine but it is
possible to enter a value in the ID textbox that does not exist in the
combobox. So I have written a procedure to check for the existence of the
value in the combobox prior to setting the value. The procedure is shown
below. My question is: Is it possible to do this without iterating
through the values, i.e. is there a single line of code using something
like .Contains that enables me to do the same thing ?


Private Sub txtCustIDEnter_Leave(ByVal sender As System.Object, ByVale
As System.EventArgs) Handles txtCustIDEnter.Leave

Dim strCustID As String = Me.txtCustIDEnter.Text.Trim.ToUpper

If strCustID.Length <> 0 Then
For Each drv As DataRowView In Me.cboCustNameSelect.ComboBox.Items
If drv.Row.Item(0).ToString.ToUpper = strCustID Then
Me.cboCustNameSelect.ComboBox.SelectedValue = strCustID
Exit For
End If
Next
End If

End Sub
 
D

diAb0Lo

I have a textbox and a combobox on a toolstrip. The user enters either an
ID in the textbox or selects a name from the combobox. When the user
selects a name from the combobox the textbox is filled in automatically by
setting its .Text property equal to the .SelectedValue of the combobox.
When the user enters an ID in the textbox the combobox's .SelectedValue is
set to the .Text value in the ID textbox. All works fine but it is
possible to enter a value in the ID textbox that does not exist in the
combobox. So I have written a procedure to check for the existence of the
value in the combobox prior to setting the value. The procedure is shown
below. My question is: Is it possible to do this without iterating
through the values, i.e. is there a single line of code using something
like .Contains that enables me to do the same thing ?

Private Sub txtCustIDEnter_Leave(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles txtCustIDEnter.Leave

Dim strCustID As String = Me.txtCustIDEnter.Text.Trim.ToUpper

If strCustID.Length <> 0 Then
For Each drv As DataRowView In Me.cboCustNameSelect.ComboBox.Items
If drv.Row.Item(0).ToString.ToUpper = strCustID Then
Me.cboCustNameSelect.ComboBox.SelectedValue = strCustID
Exit For
End If
Next
End If

End Sub

Hi...
How about to use a LIKE query: "SELECT Id FROM Table1 WHERE Id LIKE '"
+ TextBox1.Text + "%'"
 
D

DesCF

I had it mind that I didn't want to open a connection back to the server
just to check for the existence of a Customer ID when the combobox is
already populated by a valid list of Customer ID's which I could check
against. What has occurred to me is perhaps something I don't quite
understand because of my 'newness' to this, i.e. if I have a dataset with
a datatable in it that contains a list of customers how do I check against
the table in the dataset and not the table back on the server ?



Des
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top