What code should be used to focus on a TextBox?

4

What code should be used to focus on a TextBox?

I am creating a system, where it is necessary that when the client press ESC, the focus of the textBox is lost.

How do I do it?

    
asked by anonymous 03.07.2017 / 19:27

2 answers

4

You can do this in two ways:

  • Change the focus to a label , for example, since this does not perceptible to the user. As I see it, this is the best idea because it allows you to choose the next control that will get focus by clicking Tab .
  • It is also possible to change the active control from form to null .

    this.ActiveControl = null;
    
  • 03.07.2017 / 19:31
    6

    You need some other focusable control so you can change the focus, you can set to label , for example:

    private void key_pressed(object sender, KeyPressed e)  
    { 
      this.ActiveControl = label1;       
    }
    

    or to a NULL

    private void key_pressed(object sender, KeyPressed e)  
    { 
      this.ActiveControl = NULL;       
    }
    
        
    03.07.2017 / 19:32