0

I want to add a char to focused texbox in winform c#. how can i do that ? actually i want control e.KeyChar in myform_KeyPress event.

private void add_user_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '\r' && hidden_scan_textbox.Text != "")
        {
            shomare_shenasai_view.Text = hidden_scan_textbox.Text;
            hidden_scan_textbox.Text = "";
        }
        if (e.KeyChar != '\r')
        {
            hidden_scan_textbox.Text += e.KeyChar;
       //here i want to add e.KeyChar to focused texbox
            e.KeyChar = '\0';
        }
    }
Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89
hamze
  • 7,061
  • 6
  • 34
  • 43

1 Answers1

3

myFormInstance.ActiveControl returns the current focused control in the form.
Be careful because it could (depending on how your form is built) also be another control and not a textbox.

Have also a look at here: What is the preferred way to find focused control in WinForms app?

Community
  • 1
  • 1
digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • +1.. ahh ActiveControl! just couldn't remember that property, looked for FocusedControl or something :) – Renatas M. Oct 04 '11 at 08:04
  • link was so helpful problem solved ;) but with every key pressed windows play a sound. why is that ? i don't know why windows do that ! – hamze Oct 04 '11 at 08:27
  • @hamze: Sorry, no idea. We need more code to try to understand the problem, you could possibly post a new question... – digEmAll Oct 04 '11 at 08:32