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

AutoCompleteBox: Add item with TAB key

1 Answer 155 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 11 Apr 2013, 09:01 PM
Hey Telerik!

We are looking for a way to have the following functionality.  

Pressing tab while typing in the AutoCompleteTextbox will create an item.  If the tab key is pressed after an item has been created it will move the curor to the next item in the list.  If the cursor is on the last item then normal tab functionality is used and the next tabbable control has focus.

I have attached a picture illustrating all of this.

Thanks

1 Answer, 1 is accepted

Sort by
0
Accepted
Svett
Telerik team
answered on 16 Apr 2013, 01:28 PM
Hello John,

You can achieve the desired behavior by creating a custom input handler where you can alter the default behavior. You should use the following code snippet to achieve the desired behavior:
public class MyAutoCompleteBoxInputHandler : AutoCompleteBoxInputHandler
{
    public MyAutoCompleteBoxInputHandler(RadAutoCompleteBoxElement element)
        : base(element)
    {
  
    }
  
    protected override bool ProcessTabKey(KeyEventArgs e)
    {
        TextPosition position = this.TextBox.Navigator.CaretPosition;
  
        if (position.TextBlock is TextBlockElement)
        {
            RadAutoCompleteBoxElement element = this.TextBox as RadAutoCompleteBoxElement;
            this.TextBox.ViewElement.Insert(position, element.Delimiter.ToString());
            return false;
        }
        else if (!object.Equals(position, TextPosition.GetLastPosition(this.TextBox.ViewElement)))
        {
            this.TextBox.Navigator.CaretPosition = this.TextBox.Navigator.GetNextPosition(position);
            return false;
        }
  
        return base.ProcessTabKey(e);
    }
}

Then you should replace the default instance by using the following line:
this.radAutoCompleteBox1.TextBoxElement.InputHandler = new MyAutoCompleteBoxInputHandler(this.radAutoCompleteBox1.TextBoxElement as RadAutoCompleteBoxElement);

I hope that you find this information useful.

Greetings,
Svett
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
Tags
AutoCompleteBox
Asked by
John
Top achievements
Rank 1
Answers by
Svett
Telerik team
Share this question
or