我有一个文本框和组合框和一个数据表(从数据库填充) 数据表有两列,一列是 ID ,另一列是名称 组合框与此数据表绑定,如
Form1.ComboBox1.DataSource = dt
Form1.ComboBox1.DisplayMember = "name"
Form1.ComboBox1.ValueMember = "id"
如果用户从comboBox1下拉列表中选择显示成员,则会在textbox1中显示值成员
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
另一个过程是,如果用户在textbox1和textbox1的离开汉字中输入值,当ID输入textbox1并离开控件时,我们写入的内容会自动选择ComboBox1中相应的显示成员。如果不存在则清除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
答案 0 :(得分:0)
试试这个:
ComboBox1.SelectedIndex = ComboBox1.FindString(TextBox1.Text)
我每天都在使用C#,但我认为这个VB语法应该是正确的
答案 1 :(得分:0)
在TextBox1_Leave处理程序中,只需要具有以下内容:
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
Dim value As String = TextBox1.Text
ComboBox1.SelectedValue = value
End Sub