Focus on textbox in C #

0

I have a formapplication created in C #, which expects a scanned or typed data to do a search, but I want to do the automatic search if I scan the number. The automatic search already does it but only if I am positioned in the textbox, since I am not positioned in the textbox and I scan, nothing happens. So what I want is for what I scan to be placed in the textbox even if it is not positioned in it. How could he do it? I have no code about that yet. Thanks.

    
asked by use2105 02.12.2016 в 17:28
source

1 answer

3

Add an event to the Form that it detects when a key is received

private void Form1_KeyPress(object Sender, KeyPressEventArgs e)
{
     if (!textBox.Focused)
        {
            textBox.Focus();
            textBox.Text += e.KeyChar;
            // Move el cursor al final
            textBox.SelectionStart = textBox.Text.Length;
        }
}

You also have to set the KeyPreview property of the form to True:

    
answered by 02.12.2016 / 17:33
source