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

Disable automatic selection for AutoCompleteBox

2 Answers 147 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Vannick
Top achievements
Rank 1
Vannick asked on 10 May 2018, 05:23 AM

Dear support,

I am using the control RadAutoCompleteBox with Suggest mode. Everytime I press enter, the first item of the list is choosen.

Let's imagine that in my list, i have the following items: ("Hello", "Hello2").

If I start to write "He" and I click enter, the item "Hello" will be automatically selected.

This is a behaviour that I would like to avoid as I should be able to enter manuel entry. So if I write "He" and if I press enter, I would like the item "He" to be choosen in the textbox as it is my manuel input.

Is it possible to avoid this behaviour and how?

Thanks a lot

2 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 10 May 2018, 12:26 PM
Hello Vannick,

Thank you for writing. 

The default behavior of the auto-complete box is to perform a selection from the auto-complete data source if there is a match considering the inputted text and if the popup is closed by pressing the Enter key. This behavior can be altered and for the purpose, you will need to create a custom control with a special element. Then in the custom RadAutoCompleteBoxElement class, you can override the virtual OnAutoCompleteDropDownKeyDown and close the popup not performing a selection. You can also handle a case for selecting an item from the auto-complete data source as long as the inputted text matches any of the data items. Please check my code snippet below: 
public class CustomRadAutoCompleteBox : RadAutoCompleteBox
{
    public override string ThemeClassName
    {
        get
        {
            return typeof(RadAutoCompleteBox).FullName;
        }
    }
 
    protected override RadTextBoxControlElement CreateTextBoxElement()
    {
        return new CustomRadAutoCompleteBoxElement();
    }
}
 
public class CustomRadAutoCompleteBoxElement : RadAutoCompleteBoxElement
{
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(RadAutoCompleteBoxElement);
        }
    }
 
    protected override void OnAutoCompleteDropDownKeyDown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter )
        {
            string inpput = this.Text.IndexOf(this.Delimiter) > 0 ? this.Text.Substring(this.Text.LastIndexOf(this.Delimiter) + 1) : this.Text;
            foreach (RadListDataItem item in this.ListElement.Items)
            {
                if (item.Text == inpput)
                {
                    base.OnAutoCompleteDropDownKeyDown(e);
                    return;
                }
            }
 
            e.Handled = true;
            this.AutoCompleteDropDown.ClosePopup(new PopupCloseInfo(RadPopupCloseReason.Mouse, MouseEventArgs.Empty));
            return;
        }
 
        base.OnAutoCompleteDropDownKeyDown(e);
    }
}

I am also attaching a short video showing the result on my end.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Vannick
Top achievements
Rank 1
answered on 12 May 2018, 07:34 AM

Thanks a lot. I have a look with your remarks,

Have a good day

Regards

Tags
AutoCompleteBox
Asked by
Vannick
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Vannick
Top achievements
Rank 1
Share this question
or