RadAutoCompleteBox: CaretIndex is not up to date during TextChanged

2 posts, 0 answers
  1. Giovanni
    Giovanni avatar
    37 posts
    Member since:
    Feb 2011

    Posted 26 Nov 2012 Link to this post

    I want to obtain current editing text on a RadAutoCompleteBox.
    For example, on the attached screenshot I want to obtain the string "ru".

    Is there any property to read that text?

    I tried to obtain this text parsing current text, during text change, with following code:

    Private Sub RadAutoCompleteBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles RadAutoCompleteBox1.TextChanged
        If Me.RadAutoCompleteBox1.CaretIndex > 0 Then
            ' Obtain current editing text
            Dim pos As Integer = Math.Max(0, Me.RadAutoCompleteBox1.Text.LastIndexOf(";", Me.RadAutoCompleteBox1.CaretIndex) + 1)
            Me.TextBox1.Text = Me.RadAutoCompleteBox1.Text.Substring(pos, Me.RadAutoCompleteBox1.CaretIndex - pos).Trim()
        End If
    End Sub

    But the problem is that when the textchanged event fires, CaretIndex is not up-to-date: it point to the previous text position.
    Thank you for any help.

  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 29 Nov 2012 Link to this post

    Hello Giovanni,

    Thank you for writing.

    Presently, the CaretIndex position in RadAutoCompleteBox does not obey the text length of the control due to its object model. I recommend using the following code snippet to achieve the desired scenario:
    Private Sub RadAutoCompleteBox1_TextChanged(sender As System.Object, e As System.EventArgs)
        Dim navigator As AutoCompleteTextNavigator = TryCast(Me.RadAutoCompleteBox1.TextBoxElement.Navigator, AutoCompleteTextNavigator)
     
        Dim start As TextPosition = navigator.GetPreviousEditablePosition(navigator.CaretPosition)
        Dim [end] As TextPosition = navigator.GetNextEditablePosition(navigator.CaretPosition)
     
        Dim text As String = Me.RadAutoCompleteBox1.TextBoxElement.ViewElement.GetTextRange(start, [end])
    End Sub

    I hope this helps.

    Greetings,
    Svett
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top