溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

VB.NET中怎么添加自動查詢功能

發(fā)布時間:2021-08-06 15:52:41 來源:億速云 閱讀:227 作者:Leah 欄目:編程語言

VB.NET中怎么添加自動查詢功能,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

在窗體中添加如下方法實現(xiàn)VB.NET添加自動查詢功能:

***個方法是AutoCompleteKeyUp,它將組合框和KeyEventArgs對象作為參數(shù),需要在組合框的KeyUp事件中調(diào)用此方法;它全根據(jù)用戶輸入的內(nèi)容選擇最接近的內(nèi)容;

第二個方法是AutoCompleteLeave,在激活組合框的Leave事件時調(diào)用,此方法僅提取用戶最終選擇的內(nèi)容,按照組合框中的每個匹配內(nèi)容修改其大小寫。

VB.NET添加自動查詢功能的代碼如下:

  1. Private Sub AutoCompleteKeyUp(ByVal Combo As ComboBox, 
    ByVal e As KeyEventArgs)  

  2. Dim strTyped As String  

  3. Dim intFoundIndex As Integer  

  4. Dim objFoundItem As Object  

  5. Dim strFoundText As String  

  6. Dim strAppendText As String  

  7. '忽略特殊鍵  

  8. Select Case e.KeyCode  

  9. Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Down, 
    Keys.Delete, Keys.CapsLock  

  10. Return  

  11. End Select  

  12. '在查詢列表中找到  

  13. strTyped = Combo.Text  

  14. intFoundIndex = Combo.FindString(strTyped)  

  15. If intFoundIndex >= 0 Then  

  16. objFoundItem = Combo.Items(intFoundIndex)  

  17. strFoundText = Combo.GetItemText(objFoundItem)  

  18. strAppendText = strFoundText.Substring(strTyped.Length)  

  19. Combo.Text = strTyped & strAppendText  

  20. Combo.SelectionStart = strTyped.Length  

  21. Combo.SelectionLength = strAppendText.Length  

  22. End If  

  23. End Sub 


  1. Private Sub AutoCompleteLeave(ByVal Combo As ComboBox)  

  2. Dim intFoundIndex As Integer  

  3. intFoundIndex = Combo.FindStringExact(Combo.Text)  

  4. Combo.SelectedIndex = -1  

  5. Combo.SelectedIndex = intFoundIndex 

  6. End Sub  

  7. Private Sub ComboBox1_KeyUp(ByVal sender As Object, 
    ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp  

  8. AutoCompleteKeyUp(ComboBox1, e)  

  9. End Sub  

  10. Private Sub ComboBox1_Leave(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles ComboBox1.Leave  

  11. AutoCompleteLeave(ComboBox1)  

  12. End Sub  

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI