vb.net 文本框中的值成员和组合框中的显示成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16673551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:40:43  来源:igfitidea点击:

value member in textbox and display member in combobox

c#vb.netwinformsvisual-studio-2010dataview

提问by Waleed Ehsan

i have a textbox and comboboxand a datatable (fill from database) datatablehas two columns one is idand other is namethe combobox is bind with this datatable like

我有一个文本框和组合框和DataTable(填充从数据库) 数据表有两列一个ID等是名称的组合框与此类似数据表绑定

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

如果用户从 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

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

另一个过程是如果用户在 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

采纳答案by ajakblackgoat

Inside your TextBox1_Leave handler just need to have the following:

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

回答by andmat

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

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