Alternative to set focus from within the Enter event

前端 未结 3 1085
栀梦
栀梦 2021-01-22 18:27

I have a textbox and in some cases in Enter event I need to set the focus to a different textbox.

I tried that code:

 private void TextBox1_Enter(object          


        
相关标签:
3条回答
  • 2021-01-22 18:56

    Postpone executing the Focus() method until after the event is finished executing. Elegantly done by using the Control.BeginInvoke() method. Like this:

        private void textBox2_Enter(object sender, EventArgs e) {
            this.BeginInvoke((MethodInvoker)delegate { textBox3.Focus(); });
        }
    
    0 讨论(0)
  • 2021-01-22 18:58
    textBox.Select();
    

    or

    textBox.Focus();
    

    or set TabIndex = 0 from properties of that textBox.

    both methods are use to set focus on textBox in C#, .NET

    0 讨论(0)
  • 2021-01-22 19:10

    You could handle the KeyPress event instead:

    private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
       if (e.KeyChar == (char)Keys.Return)
       {
          e.Handled = true;
          TextBox2.Focus();
       }
    }
    
    0 讨论(0)
提交回复
热议问题