OnTextChanged combobox overrides

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kalvin

    #1

    OnTextChanged combobox overrides

    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.Componen tModel

    Public Class ComboBoxAutoCom plete
    Inherits System.Windows. Forms.ComboBox

    Private m_isAutoComplet eSuspended As Boolean = False
    Private m_strOriginal As String

    #Region "Properties "
    Private m_Autocomplete As Boolean = True

    <Description("E nable or disable the autocomplete feature"),
    Category("Behav ior")> _
    Public Property Autocomplete() As Boolean
    Get
    Return m_Autocomplete
    End Get
    Set(ByVal Value As Boolean)
    m_Autocomplete = Value
    If m_Autocomplete Then
    Me.DropDownStyl e = ComboBoxStyle.D ropDown
    End If
    End Set
    End Property

    #End Region

    #Region "Overrides"
    Protected Overrides Sub OnTextChanged(B yVal e As System.EventArg s)

    If Me.m_Autocomple te Then

    If Not Me.m_isAutoComp leteSuspended 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(M e.Text)

    If index > -1 Then

    ' A match is found. Select it.
    Me.SelectedInde x = index
    Me.SelectionSta rt = strlength
    Me.SelectionLen gth = Me.Text.Length

    Me.Refresh()
    End If

    End If
    End If

    MyBase.OnTextCh anged(e)

    End Sub

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

    If Me.m_Autocomple te Then
    Select Case e.KeyData
    Case Keys.Back
    If Me.SelectionSta rt > 0 Then
    Me.SelectionSta rt -= 1
    Me.SelectionLen gth += 1
    Me.m_isAutoComp leteSuspended = True

    End If

    Case Keys.Escape
    Me.m_isAutoComp leteSuspended = False
    Me.Text = Me.m_strOrigina l
    Me.SelectAll()
    Me.m_isAutoComp leteSuspended = True

    Case Else
    Me.m_isAutoComp leteSuspended = False

    End Select

    End If

    MyBase.OnKeyDow n(e)

    End Sub

    Protected Overrides Sub OnGotFocus(ByVa l e As System.EventArg s)

    If Me.m_Autocomple te Then
    Me.m_strOrigina l = Me.Text

    End If

    MyBase.OnGotFoc us(e)

    End Sub

    Protected Overrides Sub OnLostFocus(ByV al e As System.EventArg s)

    If Me.m_Autocomple te Then
    Me.m_strOrigina l = Nothing

    ' Me.SelectedInde x = me.selectedinde x
    MyBase.Selected Index = Me.SelectedInde x

    End If

    MyBase.OnLostFo cus(e)

    End Sub

    Protected Overrides Sub OnValidating(By Val e As
    System.Componen tModel.CancelEv entArgs)

    If Me.m_Autocomple te Then
    Dim index As Integer

    If Me.Text = "" Then
    Me.SelectedInde x = -1

    Else
    ' Try to find the match in the list
    index = Me.FindStringEx act(Me.Text)

    If index = -1 Then
    ' No match is found. Ask user whether to add it
    Me.OnNoMatchFou nd(e)

    Else
    ' A match is found. Select it
    Me.SelectedInde x = index

    End If

    End If

    End If

    MyBase.OnValida ting(e)

    End Sub

    #End Region

    Public Delegate Sub NoMatchFoundEve ntHandler(ByVal sender As
    System.Object, ByVal e As System.Componen tModel.CancelEv entArgs)
    Public Event NoMatchFound As NoMatchFoundEve ntHandler

    Protected Overridable Sub OnNoMatchFound( ByVal e As
    System.Componen tModel.CancelEv entArgs)
    RaiseEvent NoMatchFound(Me , e)
    End Sub

    End Class

  • Kalvin

    #2
    Re: OnTextChanged combobox overrides

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

    Kalvin

    Comment

    • Kalvin

      #3
      Re: OnTextChanged combobox overrides

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

      Kalvin

      Comment

      Working...