Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Autocomplete for winform combo box. ...
Author:
Steve Briski
E-mail:
Click to e-mail author
Submitted:
11/25/2002
Version:
VB.NET
Compatibility:
VB.NET
Category:
ActiveX
Views:
14942
Autocomplete for winform combo box.
Declarations:
'none
Code:
Module Module1 Public Sub AutoComplete(ByVal cbo As ComboBox, ByVal e As System.Windows.Forms.KeyEventArgs) Dim iIndex As Integer Dim sActual As String Dim sFound As String Dim bMatchFound As Boolean 'if backspace then remove the last character that was typed in and try to find a match. 'note that the selected text from the last character typed in to the end of the combo text field will also be deleted. If e.KeyCode = Keys.Back Then cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1) End If ' Do nothing for some keys such as navigation keys. If ((e.KeyCode = Keys.Left) Or _ (e.KeyCode = Keys.Right) Or _ (e.KeyCode = Keys.Up) Or _ (e.KeyCode = Keys.Down) Or _ (e.KeyCode = Keys.PageUp) Or _ (e.KeyCode = Keys.PageDown) Or _ (e.KeyCode = Keys.Home) Or _ (e.KeyCode = Keys.End)) Then Return End If Do ' Store the actual text that has been typed. sActual = cbo.Text ' Find the first match for the typed value. iIndex = cbo.FindString(sActual) ' Get the text of the first match. 'if index > -1 then a match was found. If (iIndex > -1) Then sFound = cbo.Items(iIndex).ToString() ' Select this item from the list. cbo.SelectedIndex = iIndex ' Select the portion of the text that was automatically ' added so that additional typing will replace it. cbo.SelectionStart = sActual.Length cbo.SelectionLength = sFound.Length bMatchFound = True Else 'if there isn't a match and the text typed in is only 1 character or nothing then just select the first 'entry in the combo box. If sActual.Length = 1 Or sActual.Length = 0 Then cbo.SelectedIndex = 0 cbo.SelectionStart = 0 cbo.SelectionLength = Len(cbo.Text) bMatchFound = True Else 'if there isn't a match for the text typed in then remove the last character of the text typed in 'and try to find a match. cbo.SelectionStart = sActual.Length - 1 cbo.SelectionLength = sActual.Length - 1 cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1) End If End If Loop Until bMatchFound End Sub End Module
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement