2014-04-23 93 views
0

請使用vb.net ultimate 2013,我有大約15個組合框在運行時使用數據填充。 現在,我使用「for循環」向組合框添加自動完成功能,並且由於「for循環」需要較長時間來加載表單,循環每個組合框以添加自動完成功能。我想到了一個有約束力的資源,但是如何實現它卻成了一個問題。有什麼快速的方法來在運行時添加自動完成功能?這是我的代碼如下。BindingSource與使用自動完成功能的組合框

SQLCon.ConnectionString = DataL1 
    SqlQuery = "Select * from Users order by UserName asc" 
    dSet1 = New DataSet : dSet2 = New DataSet : dSet3 = New DataSet : dSet4 = New DataSet 
    dSet5 = New DataSet : dSet6 = New DataSet : dSet7 = New DataSet : dSet8 = New DataSet 
    dSet9 = New DataSet : dSet10 = New DataSet : dSet11 = New DataSet : dSet12 = New DataSet 
    dSet13 = New DataSet : dSet14 = New DataSet : dSet15 = New DataSet 
    dTable = New DataSet 
    daAdapterRec = New SqlDataAdapter 
    daAdapter1 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter2 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter3 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter4 = New SqlDataAdapter(SqlQuery, SQLCon) 
    daAdapter5 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter6 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter7 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter8 = New SqlDataAdapter(SqlQuery, SQLCon) 
    daAdapter9 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter10 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter11 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter12 = New SqlDataAdapter(SqlQuery, SQLCon) 
    daAdapter13 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter14 = New SqlDataAdapter(SqlQuery, SQLCon) : daAdapter15 = New SqlDataAdapter(SqlQuery, SQLCon) 
    daAdapter = New SqlDataAdapter(SqlQuery, SQLCon) 
    Try 
     daAdapter1.Fill(dSet1, "Users") : daAdapter2.Fill(dSet2, "Users") : daAdapter3.Fill(dSet3, "Users") : daAdapter4.Fill(dSet4, "Users") : daAdapter5.Fill(dSet5, "Users") : daAdapter5.Fill(dSet5, "Users") 
     daAdapter6.Fill(dSet6, "Users") : daAdapter7.Fill(dSet7, "Users") : daAdapter8.Fill(dSet8, "Users") : daAdapter9.Fill(dSet9, "Users") : daAdapter10.Fill(dSet10, "Users") : daAdapter11.Fill(dSet11, "Users") 
     daAdapter12.Fill(dSet12, "Users") : daAdapter13.Fill(dSet13, "Users") : daAdapter14.Fill(dSet14, "Users") : daAdapter15.Fill(dSet15, "Users") 
     daAdapter.Fill(dTable, "Users") 
     'From1 
     frmtracking.cboFrom1.DataSource = dTable.Tables("Users") 
     frmtracking.cboFrom1.DisplayMember = "UserName" 
     frmTracking.cboFrom1.Text = frmVisibleNot.txtUsername.Text.Trim 

     'To1 
     frmtracking.cboTo1.DataSource = dSet1.Tables("Users") 
     frmtracking.cboTo1.DisplayMember = "UserName" 
     'To2 
     frmtracking.cboTo2.DataSource = dSet2.Tables("Users") 
     frmtracking.cboTo2.DisplayMember = "UserName" 
     'To3 
     frmtracking.cboTo3.DataSource = dSet3.Tables("Users") 
     frmtracking.cboTo3.DisplayMember = "UserName" 
     'To4 
     frmtracking.cboTo4.DataSource = dSet4.Tables("Users") 
     frmtracking.cboTo4.DisplayMember = "UserName" 
     'To5 
     frmtracking.cboTo5.DataSource = dSet5.Tables("Users") 
     frmtracking.cboTo5.DisplayMember = "UserName" 
     'To6 
     frmtracking.cboTo6.DataSource = dSet6.Tables("Users") 
     frmtracking.cboTo6.DisplayMember = "UserName" 
     'To7 
     frmtracking.cboTo7.DataSource = dSet7.Tables("Users") 
     frmtracking.cboTo7.DisplayMember = "UserName" 
     'To8 
     frmtracking.cboTo8.DataSource = dSet8.Tables("Users") 
     frmtracking.cboTo8.DisplayMember = "UserName" 
     'To9 
     frmtracking.cboTo9.DataSource = dSet9.Tables("Users") 
     frmtracking.cboTo9.DisplayMember = "UserName" 
     'To10 
     frmtracking.cboTo10.DataSource = dSet10.Tables("Users") 
     frmtracking.cboTo10.DisplayMember = "UserName" 
     'To11 
     frmtracking.cboTo11.DataSource = dSet11.Tables("Users") 
     frmtracking.cboTo11.DisplayMember = "UserName" 
     'To12 
     frmtracking.cboTo12.DataSource = dSet12.Tables("Users") 
     frmtracking.cboTo12.DisplayMember = "UserName" 
     frmtracking.cboTo12.Text = "" 
     'To13 
     frmtracking.cboTo13.DataSource = dSet13.Tables("Users") 
     frmtracking.cboTo13.DisplayMember = "UserName" 
     'To14 
     frmtracking.cboTo14.DataSource = dSet14.Tables("Users") 
     frmtracking.cboTo14.DisplayMember = "UserName" 
     'To15 
     frmtracking.cboTo15.DataSource = dSet15.Tables("Users") 
     frmTracking.cboTo15.DisplayMember = "UserName" 
     'Auto Complete All the Fields 
     Dim col As New AutoCompleteStringCollection 
     Dim i As Integer 
     For i = 0 To dTable.Tables(0).Rows.Count - 1 
      col.Add(dTable.Tables(0).Rows(i)("UserName").ToString.Trim()) 
      frmTracking.cboTo1.AutoCompleteSource = AutoCompleteSource.CustomSource 
      frmTracking.cboTo1.AutoCompleteCustomSource = col 
      frmTracking.cboTo1.AutoCompleteMode = AutoCompleteMode.Suggest 
     Next 

     'Auto Complete All the Fields 
     Dim col2 As New AutoCompleteStringCollection 
     Dim ii As Integer 
     For ii = 0 To dSet2.Tables(0).Rows.Count - 1 
      col2.Add(dSet2.Tables(0).Rows(ii)("UserName").ToString.Trim()) 
      frmTracking.cboTo2.AutoCompleteSource = AutoCompleteSource.CustomSource 
      frmTracking.cboTo2.AutoCompleteCustomSource = col2 
      frmTracking.cboTo2.AutoCompleteMode = AutoCompleteMode.Suggest 
     Next 
     'Auto Complete All the Fields 
     Dim col3 As New AutoCompleteStringCollection 
     Dim iii As Integer 
     For iii = 0 To dSet3.Tables(0).Rows.Count - 1 
      col3.Add(dSet3.Tables(0).Rows(iii)("UserName").ToString.Trim()) 
      frmTracking.cboTo3.AutoCompleteSource = AutoCompleteSource.CustomSource 
      frmTracking.cboTo3.AutoCompleteCustomSource = col3 
      frmTracking.cboTo3.AutoCompleteMode = AutoCompleteMode.Suggest 
     Next 
     'Auto Complete All the Fields 
     Dim col4 As New AutoCompleteStringCollection 
     Dim iv As Integer 
     For iv = 0 To dSet4.Tables(0).Rows.Count - 1 
      col4.Add(dSet4.Tables(0).Rows(iv)("UserName").ToString.Trim()) 
      frmTracking.cboTo4.AutoCompleteSource = AutoCompleteSource.CustomSource 
      frmTracking.cboTo4.AutoCompleteCustomSource = col4 
      frmTracking.cboTo4.AutoCompleteMode = AutoCompleteMode.Suggest 
     Next 
     'Auto Complete All the Fields 
     Dim col5 As New AutoCompleteStringCollection 
     Dim v As Integer 
     For v = 0 To dSet5.Tables(0).Rows.Count - 1 
      col5.Add(dSet5.Tables(0).Rows(v)("UserName").ToString.Trim()) 
      frmTracking.cboTo5.AutoCompleteSource = AutoCompleteSource.CustomSource 
      frmTracking.cboTo5.AutoCompleteCustomSource = col5 
      frmTracking.cboTo5.AutoCompleteMode = AutoCompleteMode.Suggest 
     Next 
     Dim col6 As New AutoCompleteStringCollection 
     Dim vi As Integer 
     For vi = 0 To dSet6.Tables(0).Rows.Count - 1 
      col6.Add(dSet6.Tables(0).Rows(vi)("UserName").ToString.Trim()) 
      frmTracking.cboTo6.AutoCompleteSource = AutoCompleteSource.CustomSource 
      frmTracking.cboTo6.AutoCompleteCustomSource = col6 
      frmTracking.cboTo6.AutoCompleteMode = AutoCompleteMode.Suggest 
     Next 


     Dim col7 As New AutoCompleteStringCollection 
     Dim vii As Integer 
     For vii = 0 To dSet7.Tables(0).Rows.Count - 1 
      col7.Add(dSet7.Tables(0).Rows(vii)("UserName").ToString.Trim()) 
      frmTracking.cboTo7.AutoCompleteSource = AutoCompleteSource.CustomSource 
      frmTracking.cboTo7.AutoCompleteCustomSource = col7 
      frmTracking.cboTo7.AutoCompleteMode = AutoCompleteMode.Suggest 
     Next 

     Dim col8 As New AutoCompleteStringCollection 
     Dim viii As Integer 
     For viii = 0 To dSet8.Tables(0).Rows.Count - 1 
      col8.Add(dSet8.Tables(0).Rows(viii)("UserName").ToString.Trim()) 
      frmTracking.cboTo8.AutoCompleteSource = AutoCompleteSource.CustomSource 
      frmTracking.cboTo8.AutoCompleteCustomSource = col8 
      frmTracking.cboTo8.AutoCompleteMode = AutoCompleteMode.Suggest 
     Next 

     Dim col9 As New AutoCompleteStringCollection 
     Dim ix As Integer 
     For ix = 0 To dSet9.Tables(0).Rows.Count - 1 
      col9.Add(dSet9.Tables(0).Rows(ix)("UserName").ToString.Trim()) 
      frmTracking.cboTo9.AutoCompleteSource = AutoCompleteSource.CustomSource 
      frmTracking.cboTo9.AutoCompleteCustomSource = col9 
      frmTracking.cboTo9.AutoCompleteMode = AutoCompleteMode.Suggest 
     Next 

回答

0

看起來你正在重複你的代碼太多次。
未測試

SQLCon.ConnectionString = DataL1 
SqlQuery = "Select * from Users order by UserName asc" 
ds = New DataSet 
da = New SqlDataAdapter(SqlQuery, SQLCon) 
Try 
    da.Fill(ds, "Users") 
    Dim bs = new BindingSource() 
    bs.DataSource = ds.Tables(0) 
    frmtracking.cboFrom1.DataSource = bs 

    Dim bs1 = new BindingSource() 
    bs1.DataSource = ds.Tables(0) 
    frmtracking.cboFrom2.DataSource = bs1 

    .... repeat for the other combos 


    Dim col As New AutoCompleteStringCollection 
    Dim i As Integer 
    For i = 0 To dTable.Tables(0).Rows.Count - 1 
     col.Add(dTable.Tables(0).Rows(i)("UserName").ToString.Trim()) 
    Next 
    frmTracking.cboTo1.AutoCompleteSource = AutoCompleteSource.CustomSource 
    frmTracking.cboTo1.AutoCompleteCustomSource = col 
    frmTracking.cboTo1.AutoCompleteMode = AutoCompleteMode.Suggest 


    frmTracking.cboFrom1.AutoCompleteSource = AutoCompleteSource.CustomSource 
    frmTracking.cboFrom1.AutoCompleteCustomSource = col 
    frmTracking.cboFrom1.AutoCompleteMode = AutoCompleteMode.Suggest 

    ... repeat for the other combos 
+0

感謝您的答覆,但組合框沒有得到填充,自動完成不能正常工作。只是感到困惑。請檢查我的代碼 – Benniit

+0

首先檢查您的da.Fill(ds ..)是否返回表中的某些行。填充後設置斷點並查找'ds.Tables(0).Rows.Count'值。那你確定frmTracking變量是一個當前可見形式的實例嗎?我想這個代碼是在一些從外部調用接收變量的類中。相反,如果你在frmTracking表單類中,那麼改變對Me.cboFrom1等的引用.... – Steve