Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"autocomplete" routine for combobox?

5 views
Skip to first unread message

ben coats

unread,
Feb 17, 2003, 5:28:13 PM2/17/03
to
hey!
i'm actually reposting this question, which i had up a
while back...

in MSAccess the combobox control had a property called
AutoComplete, which, when turned on, would fill the field
with the closest match from the list to whatever the user
was typing. the characters that the user had not yet
entered, but were from that matching item, were
highlited. i want to do something similar with some
combobox controls in vb.net. the thing is, i also want
the contents of the control to be checked against the
list and, if not in list, i want the user to be asked
whether they wish to add the item.

now, with the .DropDownStyle property of the combobox set
to DropDownList it will "complete" the text you are
entering with the closest match...however, when this is
set, you can ONLY select items from the list; you can't
enter items not in the list. if you set the property to
DropDown, then it will not "complete" your entry, but you
CAN add text not in the list. i need to find a happy
median, where i can do BOTH! i have comboboxes set to
DropDown, and have event methods written so that it will
check the contents and ask the user whether to add an
item not in the list, but what i want to know is, have
any of you written a method, or would know how to write
one, that would do the autocomplete thing for
you...something i could call (after a few validations)
from the TextChanged event?

any help on this would be greatly appreciated!!!
thanks!
ben

Programatix

unread,
Feb 18, 2003, 3:41:43 AM2/18/03
to
You'll have to create a new control which inherits the ComboBox control.

Here's a simple sample I've written and used in my work,
Option Strict On

Imports System.ComponentModel

Public Class ComboBoxAutoComplete

Inherits System.Windows.Forms.ComboBox

Private m_isAutoCompleteSuspended As Boolean = False

Private m_strOriginal As String

#Region "Properties"

Private m_Autocomplete As Boolean = True

<Description("Enable or disable the autocomplete feature"),
Category("Behavior")> _

Public Property Autocomplete() As Boolean

Get

Return m_Autocomplete

End Get

Set(ByVal Value As Boolean)

m_Autocomplete = Value

If m_Autocomplete Then

Me.DropDownStyle = ComboBoxStyle.DropDown

End If

End Set

End Property

#End Region

#Region "Overrides"

Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)

MyBase.OnTextChanged(e)

If Me.m_Autocomplete Then

If Not Me.m_isAutoCompleteSuspended Then

Dim index As Integer

Dim strlength As Integer

' Retrieve the current text's length

strlength = Me.Text.Length

' Try to find the match in the list

index = Me.FindString(Me.Text)

Try

If index > -1 Then

' A match is found. Select it.

Me.SelectedIndex = index

Me.SelectionStart = strlength

Me.SelectionLength = Me.Text.Length

End If

Catch

End Try

End If

End If

End Sub

Protected Overrides Sub OnKeyDown(ByVal e As
System.Windows.Forms.KeyEventArgs)

If Me.m_Autocomplete Then

Select Case e.KeyData

Case Keys.Back

If Me.SelectionStart > 0 Then

Me.SelectionStart -= 1

Me.SelectionLength += 1

Me.m_isAutoCompleteSuspended = True

End If

Case Keys.Escape

Me.m_isAutoCompleteSuspended = False

Me.Text = Me.m_strOriginal

Me.SelectAll()

Me.m_isAutoCompleteSuspended = True

Case Else

Me.m_isAutoCompleteSuspended = False

End Select

End If

MyBase.OnKeyDown(e)

End Sub

Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)

If Me.m_Autocomplete Then

Me.m_strOriginal = Me.Text

End If

MyBase.OnGotFocus(e)

End Sub

Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)

If Me.m_Autocomplete Then

Me.m_strOriginal = Nothing

End If

MyBase.OnLostFocus(e)

End Sub

Protected Overrides Sub OnValidating(ByVal e As
System.ComponentModel.CancelEventArgs)

If Me.m_Autocomplete Then

Dim index As Integer

If Me.Text = "" Then

Me.SelectedIndex = -1

Else

' Try to find the match in the list

index = Me.FindStringExact(Me.Text)

If index = -1 Then

' No match is found. Ask user whether to add it

Me.OnNoMatchFound(e)

Else

' A match is found. Select it

Me.SelectedIndex = index

End If

End If

End If

MyBase.OnValidating(e)

End Sub

#End Region

Public Delegate Sub NoMatchFoundEventHandler(ByVal sender As System.Object,
ByVal e As System.ComponentModel.CancelEventArgs)

Public Event NoMatchFound As NoMatchFoundEventHandler

Protected Overridable Sub OnNoMatchFound(ByVal e As
System.ComponentModel.CancelEventArgs)

RaiseEvent NoMatchFound(Me, e)

End Sub

End Class


"ben coats" <im4evri...@hotmail.com> wrote in message
news:016b01c2d6d3$dbd7c630$a601...@phx.gbl...

ben coats

unread,
Feb 18, 2003, 8:12:30 AM2/18/03
to
thanks so much! that looks like it will work great!
ben
>.
>
0 new messages