I've just implemented a quick autocomplete feature in a textbox that pulls a string out of a fairly small list and "completes" the word. The TextBox caret stays at the location it was in from the last keypress, and the part of the word that the user has yet to type becomes highlighted, such that beginning to type something else will remove this section of the input.
The stickler is that I need to have it such that, upon completion and part-highlighting, the space bar works as an "accept" key - for instance it'd move the caret to the end of the completed word. However no matter what I seem to do hitting space deletes the highlighted part of the word (replacing it with a space character, just as if you'd hit any other key).
I've tried this:
private void textBoxIncidentLogTypes_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
textBoxIncidentLogTypes.CaretIndex = textBoxIncidentLogTypes.Text.Length;
}
}
But while this "works" it gets fired after the space key has demolished the best part of a phrase. Is their any way to capture the keypress before it gets typed into the textbox?