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 combobox - Version 2...
Author:
Steve Briski
E-mail:
Click to e-mail author
Submitted:
4/10/2003
Version:
VB.NET
Compatibility:
VB.NET
Category:
Forms
Views:
25191
Autocomplete for winform combobox - Version 2
Declarations:
'none
Code:
Take this and put it into its own class library project. I had a need to "turn off" the autocomplete functionality if the user entered a wildcard character (%,_, etc.). If you have a need for something like this then modify it as needed, otherwise remove the property and any references to wildcard. The wildcard property defaults to false anyway. It shows you have to add custom properties to your control. See Francesco's book. Compile it and add the new control to your toolbox. Then you can just drop it on any form that needs it. Make a reference to system.drawing and system.windows.forms and system.drawing. Imports System.ComponentModel Public Class ComboBoxAutoComplete Inherits System.Windows.Forms.ComboBox Private _WildCardSearch As Boolean
_ Property WildCardSearch() As Boolean Get Return _WildCardSearch End Get Set(ByVal Value As Boolean) _WildCardSearch = Value End Set End Property Private Sub ComboBoxAutoComplete_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp Dim WildcardCharacters() As String = {"%", "_", "<", ">"} Dim Count As Integer 'If the combo box is used for wildcard searches then don't do the autocomplete if a wildcard character has been entered. If WildCardSearch Then For Count = 0 To WildcardCharacters.Length - 1 If Me.Text.IndexOf(WildcardCharacters(Count)) > -1 Then Return End If Next End If AutoComplete(Me, e) End Sub Private Sub AutoComplete(ByVal cbo As ComboBox, ByVal e As System.Windows.Forms.KeyEventArgs) Dim Index As Integer Dim Actual As String Dim Found As String Dim MatchFound As Boolean 'If the contents of the combo have been removed then select the first entry in the combo box list. cbo.Text = cbo.Text.Trim If cbo.Text.Length = 0 Then cbo.SelectedIndex = 0 cbo.SelectAll() Return End If 'If the backspace key was pressed 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 = cbo.Text.Substring(0, cbo.Text.Length - 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. Actual = cbo.Text ' Find the first match for the typed value. Index = cbo.FindString(Actual) ' Get the text of the first match. 'if index > -1 then a match was found. If (Index > -1) Then Found = cbo.Items(Index).ToString() ' Select this item from the list. cbo.SelectedIndex = Index ' Select the portion of the text that was automatically added so that any additional typing will replace it. cbo.SelectionStart = Actual.Length cbo.SelectionLength = Found.Length MatchFound = True Else 'If there isn't a match and the text typed in is only one character or nothing then just select the first 'entry in the combo box. If Actual.Length = 1 Or Actual.Length = 0 Then cbo.SelectedIndex = 0 cbo.SelectAll() MatchFound = 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 = Actual.Length - 1 cbo.SelectionLength = Actual.Length - 1 cbo.Text = cbo.Text.Substring(0, cbo.Text.Length - 1) End If End If Loop Until MatchFound End Sub End Class
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement