2014-07-15 182 views
0

我有一個名爲EmployedCombobox1的「下拉」樣式組合框。我試圖讓EmployedComobox1根據寫在另一個表單上的文本框中的文本自動選擇一個值。基於Textbox.Text選擇組合框值

下面的代碼是我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() 

當我運行第二個代碼,當在文本框中寫入文本時,在組合框中沒有任何選擇。

+0

這條線困擾我。我會首先創建一個字符串,以確保它位於組合項的第一個內: – Kat

+0

執行類似Dim y =(sqlTable1.Rows(i)(「institution_name」))。 – Kat

+0

這將給我一個錯誤,指出「從字符串轉換爲整型無效」 – AznDevil92

回答

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 
相關問題