C# – How get Event “item Selected” with AutoComplete in C#

autocompletec++

I have code using an AutoCompleteStringCollection:

    private void txtS_TextChanged(object sender, EventArgs e)
    {
        TextBox t = sender as TextBox;
        string[] arr = this.dbService.GetAll();

        if (t != null)
        {
            if (t.Text.Length >= 3)
            {
                AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
                collection.AddRange(arr);                    
                this.txtSerial.AutoCompleteCustomSource = collection;
            }
        }
    }

How can I get the event for "item selected" after user selects an AutoComplete suggestion? And value of field?

Best Solution

There's no such thing as chosen item Event for a textBox, which I believe you're using for the AutoComplete. What you could do is add a key down event to your textBox. There you could verify if the enter key was pressed (clicking on a suggested link is the same as pressing enter). Something like that:

private void textBox1_KeyDown(object sender, KeyEventArgs e) {
    if (e.KeyData == Keys.Enter) {
        String selItem = this.textBox1.Text;
    }
}