选择基于Textbox.Text的Combobox值

时间:2014-07-15 19:19:49

标签: vb.net combobox

我有一个名为EmployedCombobox1的'Drop Down'风格组合框。我试图让EmployedComobox1自动选择一个值,具体取决于在另一个表单上的文本框中写入的文本。

以下代码是我的EmployedCombobox的填充方式:

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()

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

1 个答案:

答案 0 :(得分:0)

就像你说的值是相同的,所以我会为文本框制作一个AutoCompleteStringCollection。

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

然后,对于选择,我将使用AutoCompleteMode.Suggest,否则如果您在文本框中键入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_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