I want to focus a textbox when a key is pressed. I use this code:
private void MainForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
textBoxCode.Focus();
}
With KeyPreview = true on my form. But when I do that, if I write 'az', only the 'z' char appear in my textbox. If I press only 'a', textboxCode is empty but have the focus.
How not to lose the key pressed?
SOLUTION:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (textBox1.Focused == false)
{
textBox1.Text += e.KeyChar.ToString();
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.Focus();
}
}