OnTextChanged combobox overrides

K

Kalvin

I found some code in Google, don't remember where, for an AutoComplete
combobox. Everything is great with it except for one thing. If I use
the mouse to drop the list down, then start typing to find the item in
the list, the visible text changes, but the values of me.Text and
Mybase.Text do not change. If you use the mouse and actually click on
the item, the values of me.text and Mybase.Text will change. Or, if
you use the enter key to move focus to the next box the value of Text
will be updated. Is there something I am doing wrong here. I thought
the visible text you are seeing on the screen would have to be the
value of MyBase.Text. Since they aren't the same value, how is the
visible Text being stored to be able to display it on the screen?

Any help will be very appreciated.

Thanks
Kalvin


'****** begin code *******

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)

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)

If index > -1 Then

' A match is found. Select it.
Me.SelectedIndex = index
Me.SelectionStart = strlength
Me.SelectionLength = Me.Text.Length

Me.Refresh()
End If

End If
End If

MyBase.OnTextChanged(e)

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

' Me.SelectedIndex = me.selectedindex
MyBase.SelectedIndex = Me.SelectedIndex

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
 
K

Kalvin

Additional note about this. It appears to only happen when the
combobox is bound.

Kalvin
 
K

Kalvin

Is there somewhere I can post this that someone may know what I can do
about this?

Kalvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top