我有一个文本框和组合框和一个数据表(从数据库填充)数据 表有两列,一列是id,另一列是名称 组合框与此数据表绑定,例如
Form1.ComboBox1.DataSource = dt
Form1.ComboBox1.DisplayMember = "name"
Form1.ComboBox1.ValueMember = "id"
如果用户从 comboBox1 下拉列表中选择显示成员,则 valuemember 显示在 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