This is a migrated thread and some comments may be shown as answers.

RadAutoCompleteBox: CaretIndex is not up to date during TextChanged

1 Answer 82 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Giovanni
Top achievements
Rank 1
Iron
Iron
Iron
Giovanni asked on 26 Nov 2012, 11:57 AM
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.

1 Answer, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 29 Nov 2012, 07:39 AM
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.
Tags
AutoCompleteBox
Asked by
Giovanni
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Svett
Telerik team
Share this question
or