文本框中值成员,组合框中显示成员

本文关键字:成员 显示 组合 文本 | 更新日期: 2023-09-27 18:14:45

我有一个文本框和组合框和一个数据表(从数据库填充)datatable有两列,一列是id,另一列是name组合框与这个数据表绑定,如

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

如果用户从comboBox1中选择一个显示成员,则valuemmember显示在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的leave处理程序中输入我们所写的,当ID输入textbox1和leave控件时,它会自动选择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

文本框中值成员,组合框中显示成员

在你的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

try this:

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

我每天都在用c#工作,但我认为这个VB语法应该是正确的