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

Autocomplete combined with fulltext search

4 Answers 325 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 10 Nov 2009, 03:45 PM
Hi!

I'm trying to realize some kind of article search in form of a text- or combobox. When the user enters some text, the autocompletion should perform a fulltext search for any matching article.

The actual autocompletion only works like the string.StartsWith(...) function but I'd like to have something like the string.Contains(...) function.

I tried to ralize it with a combobox that changes its ItemCollection on the TextChanged event:

private

 

void radComboBox1_TextChanged(object sender, EventArgs e)

 

{

    var query = from s in _autoComplete.Keys  
            where s.Contains(radComboBox1.Text)  
            select s;  
 
    radComboBox1.BeginUpdate();  
    radComboBox1.Items.Clear();  
 
    foreach (string art in query.ToArray())  
    {  
        radComboBox1.Items.Add(new RadComboBoxItem() { Text = art, Tag = _autoComplete[art] });  
    }  
 
    radComboBox1.EndUpdate();  
    radComboBox1.ShowDropDown(); 
}

This work to the point where the user uses the arrow keys to navigate through the autocomplete list. This leads to an update and only one remaining option in the autocomplete list.

Any suggestions? Is there a property for the autocomplete functionality I don't know?

kind regards,
Thomy

4 Answers, 1 is accepted

Sort by
0
Accepted
Deyan
Telerik team
answered on 13 Nov 2009, 01:50 PM
Hello Thomas,

Thanks for writing.

The reason for the undesired behavior that you experience is that when navigating through the items in the RadComboBox control, the TextChanged event is fired since the text is updated to reflect the currently selected item. The current implementation of this event in your code takes the text from the combo box (which will exactly match an item from the drop down), filters the options again, and thus only one item remains visible in the drop down.

In this case, you can use the KeyPress event of the RadComboBox instead and then apply the filtering. The KeyPress event will not be fired when you navigate over the items in the drop down and therefore you will not face the issue described above.

I hope this is helpful.

You can write back anytime you need further assistance.

All the best,
Deyan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Thomas
Top achievements
Rank 1
answered on 17 Nov 2009, 07:45 AM
Hello Deyan!

Thanks for your help, the proposed solution works great!

kind regards,
Thomy
0
NORIS
Top achievements
Rank 1
answered on 24 Feb 2010, 02:46 PM
I would like to use this in VB.  I used the code convert and it does not like var as a type.
Could you tell me how to fix this?
Thanks.

 

Public Sub FilterList(ByVal cb As RadComboBox) 'use on KeyPress event

 

 

'filter in list of combobox with Contains

 

 

Dim query As var = From s In _autoComplete.Keys _

 

 

Where s.Contains(cb.Text) _

 

 

Select s

 

cb.BeginUpdate()

cb.Items.Clear()

 

For Each art As String In query.ToArray()

 

cb.Items.Add(

New RadComboBoxItem())

 

 

Next

 

cb.EndUpdate()

cb.ShowDropDown()

 

End Sub 'FilterList

 

0
Nikolay
Telerik team
answered on 02 Mar 2010, 03:12 PM
Hi NORIS,

Var is a special keyword in C# and you can find additional information about it here. However, you do not need it in VB.NET and you can make a query as it is demonstrated in this article.

Best wishes,
Nikolay
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
AutoCompleteBox
Asked by
Thomas
Top achievements
Rank 1
Answers by
Deyan
Telerik team
Thomas
Top achievements
Rank 1
NORIS
Top achievements
Rank 1
Nikolay
Telerik team
Share this question
or