WinForms AutoCompleteMode in RadTextBoxControl

4 Answers 61 Views
AutoCompleteBox TextBox
Jacek
Top achievements
Rank 2
Iron
Iron
Iron
Jacek asked on 27 Sep 2022, 09:40 PM | edited on 27 Sep 2022, 09:42 PM

I need AutoComplete functionality for RadTextBoxControl that will allow me to identify the object selected by the user.


Public Class Person
    Public Property GuidPerson As Guid
    Public Property FirstName As String
    Public Property LastName As String
    Public ReadOnly Property FullName As String
        Get
            Return String.Format("{0} {1}", FirstName, LastName)
        End Get
    End Property
    Public Sub New(fName As String, lname As String)
        GuidPerson = Guid.NewGuid
        FirstName = fName
        LastName = lname
    End Sub
End Class

Public Class RadForm4
    Private listPerson As New List(Of Person)
    Public Sub New()
        InitializeComponent()

        listPerson.Add(New Person("John", "Snow"))
        listPerson.Add(New Person("Arya", "Stark"))
        listPerson.Add(New Person("Brandon", "Stark"))
        listPerson.Add(New Person("Catelyn", "Stark"))

        RadTextBoxControl1.AutoCompleteMode = AutoCompleteMode.Suggest
        RadTextBoxControl1.AutoCompleteDataSource = listPerson
        RadTextBoxControl1.AutoCompleteDisplayMember = "FullName"

    End Sub
    Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
        'HOW TAKE 'GuidPerson' from selected item in RadTextBoxControl1
    End Sub
End Class

How to capture a user-selected object?

For example, in RadDropDownList I would do it like this:

Dim searchValue As Person = TryCast(RadDropDownList1.SelectedItem.DataBoundItem, Person)

I don't want to use RadAutoCompleteBox because I want to display the results text and not tokens

Regards

Jack


 

 

4 Answers, 1 is accepted

Sort by
1
Accepted
Maria
Telerik team
answered on 03 Oct 2022, 12:43 PM

Hi Jacek, 

To extract the Person object and get the GUID value, you can subscribe to the SelectedIndexChanged event of the RadTextBoxControl.ListElement. You can use the ListElement property and its Items collection which gets the auto-complete items which DataBoundItem property stores the respective Person.

AddHandler Me.RadTextBoxControl1.ListElement.SelectedIndexChanged, AddressOf ListElement_SelectedIndexChanged

Private Sub ListElement_SelectedIndexChanged(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Data.PositionChangedEventArgs)
        If e.Position > -1 Then
            Dim p As Person = TryCast(Me.RadTextBoxControl1.ListElement.Items(e.Position).DataBoundItem, Person)
            Me.Text = p.GuidPerson.ToString()
        End If
End Sub

 

I am sending you the example project as well!

Hope this helps you!

Regards,
Maria
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Maria
Telerik team
answered on 29 Sep 2022, 10:43 AM

Hi Jacek,

Thank you for the code snippet.

The RadTextBoxControl doesn't have the SelectedItem property as the DropDownList as you wrote. One way to get the GUID from the selected item in RadTextBoxControl is to use LINQ query to iterate the  List collection. I am sending you a code snippet for this written in the button, where you left the comment.

 Private Sub radButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim text = radTextBoxControl1.SelectedText
        Dim person = listPerson.FirstOrDefault(Function(x) x.FullName = text)
        Dim guid = person.GuidPerson
    End Sub

I am sending you a project as well.

I hope this helps.

Regards,
Maria
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Jacek
Top achievements
Rank 2
Iron
Iron
Iron
answered on 29 Sep 2022, 12:28 PM | edited on 29 Sep 2022, 12:29 PM

Hi Maria,
Thank you for your code, however this may cause a bug with two records of the same value.
I made my example based on the Name and Surname to explain the problem more easily, but in the case of e.g. company names, there is a possibility of two records with the same name.
Is there another solution to the problem? For example, I was looking for the option to disable tokens in RadAutoCompleteBox but I did not find such a setting.

Regards

Jack
0
Jacek
Top achievements
Rank 2
Iron
Iron
Iron
answered on 05 Oct 2022, 07:50 PM
Hi Maria,

Thank you very much.
This is what I was looking for.

Regards
Jack
Tags
AutoCompleteBox TextBox
Asked by
Jacek
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Maria
Telerik team
Jacek
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or