Vb.net 根据Textbox.Text选择Combobox值

Vb.net 根据Textbox.Text选择Combobox值,vb.net,combobox,Vb.net,Combobox,我有一个名为EmployedCombobox1的“下拉式”组合框。我正试图让EmployedComobox1根据另一表单上的文本框中写入的文本自动选择一个值 下面的代码是如何填充my EmployeedComboBox的: Private Sub getinstitutionname(ByVal p_institution1_id As Integer) If sConnection.State = ConnectionState.Closed Then sConnect

我有一个名为EmployedCombobox1的“下拉式”组合框。我正试图让EmployedComobox1根据另一表单上的文本框中写入的文本自动选择一个值

下面的代码是如何填充my EmployeedComboBox的:

Private Sub getinstitutionname(ByVal p_institution1_id As Integer)
    If sConnection.State = ConnectionState.Closed Then
        sConnection.ConnectionString = Search.sqlConnect
        sConnection.Open()
    End If

    Dim sqlAdapter As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim sqlTable As New DataTable
    Dim InstitutionName As String

    Dim sqlText As String = "select * from institution order by institution_name"
    Dim InstitutionID As Integer
    With sqlCommand
        .CommandText = sqlText
        .Connection = sConnection
    End With

    With sqlAdapter
        .SelectCommand = sqlCommand
        .Fill(sqlTable)
    End With

    EmployedComboBox1.Items.Clear()
    EmployedComboBox1.SelectedIndex = -1

    For i = 0 To sqlTable.Rows.Count - 1
        InstitutionName = sqlTable.Rows(i)("institution_name")
        InstitutionID = sqlTable.Rows(i)("institution_id")
        EmployedComboBox1.Items.Add(InstitutionName)
        If p_institution1_id = InstitutionID Then
            EmployedComboBox1.SelectedIndex = i
        End If
    Next
    sqlTable.Dispose()
    sqlCommand.Dispose()
    sqlAdapter.Dispose()
接下来,下面的代码是我试图根据textbox自动选择值。text:

    Dim sqlAdapter1 As New MySqlDataAdapter
    Dim sqlCommand1 As New MySqlCommand
    Dim sqlTable1 As New DataTable

    Dim sqlText1 As String = "select institution_name from institution where institution_name='" & Institution.InstitutionNameTextBox.Text & "'"

    If Search.debugging = True Then
        MsgBox(sqlText1)
    End If

    With sqlCommand1
        .CommandText = sqlText1
        .Connection = sConnection
    End With

    With sqlAdapter1
        .SelectCommand = sqlCommand1
        .Fill(sqlTable1)
    End With

    For i = 0 To sqlTable1.Rows.Count - 1
        Me.EmployedComboBox1.SelectedItem = (sqlTable1.Rows(i)("institution_name"))
    Next
    sqlTable1.Dispose()
    sqlCommand1.Dispose()
    sqlAdapter1.Dispose()

当我运行第二个代码时,当文本框中写入文本时,组合框中不会选择任何内容。

正如您所说,值是相同的,因此我将为文本框创建一个AutoCompleteTestRingCollection。

在页面加载时,将组合框中的项目也放在AutoCompleteTestringCollection中。“或者在加载后即为组合框” 然后将其添加到文本框中

然后对于选择,我将使用自动完成模式。否则,如果在文本框中键入1个字母,它将在组合框中选择一个项目

Dim names As New AutoCompleteStringCollection()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ComboBox1.Items.AddRange({"yes", "no", "mmm"})

    For Each item As String In ComboBox1.Items
        names.Add(item)
    Next

    With TextBox1
        .AutoCompleteMode = AutoCompleteMode.Suggest
        .AutoCompleteCustomSource = names
        .AutoCompleteSource = AutoCompleteSource.CustomSource
    End With
End Sub
对于组合框项目的选择,首先检查组合框中是否存在文本框文本,然后选择它。“如下面显示的代码”
我使用了TextBox1\u TextChanged事件来处理它。

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    If ComboBox1.Items.Contains(TextBox1.Text) = True Then
        ComboBox1.SelectedIndex = (ComboBox1.FindString(TextBox1.Text))
    End If
End Sub

这句话让我很烦。我会首先创建一个字符串,以确保它在组合的项目中:执行Dim y=(sqlTable1.Rows(I)(“institution_name”))之类的操作。这会给我一个错误,说明“从字符串到整型的转换无效”。当sql行为整型时,您是否将y设置为字符串,反之亦然?这可能是你在其他地方的问题。“(sqlTable1.Rows(i)(“institution_name”)”与combobox.selecteditem的变量类型不同。尝试选择索引或强制转换它。你在文本框中写什么??与组合框项目相同??