0
votes

i have a textbox and combobox and a datatable (fill from database) datatable has two columns one is id and other is name the combobox is bind with this datatable like

Form1.ComboBox1.DataSource = dt
    Form1.ComboBox1.DisplayMember = "name"
    Form1.ComboBox1.ValueMember = "id"

if the user select a display member from comboBox1 dropdown the valuemember display in the textbox1 like

Private Sub ComboBox1_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
    If ComboBox1.SelectedIndex = -1 Then
        Return
    Else
        TextBox1.Text = ComboBox1.SelectedValue.ToString
    End If

and the other process is if the user enter the value in textbox1 and in the leave hanler of textbox1 what we write that when an ID enter in the textbox1 and leave control its automatically selected the corresponding display member in ComboBox1. if not exist then clear the textbox1

Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

    Dim dv As DataView
    if ( dv = dv.RowFilter = "id =" & TextBox1.Text.ToString) then
//select the value memeber if record find
//ComboBox1.text = finded diaplay member 
else
textbox1.text = string.empty
ComboBox1.selectindex = -1
end if
End Sub
2

2 Answers

0
votes

try this:

ComboBox1.SelectedIndex = ComboBox1.FindString(TextBox1.Text)

I'm working with C# on a daily basis but I think this VB syntax should be right

0
votes

Inside your TextBox1_Leave handler just need to have the following:

Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
    Dim value As String = TextBox1.Text
    ComboBox1.SelectedValue = value
End Sub