开发者

Alternative to set focus from within the Enter event

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 sender, EventArgs e)
 {
     if(_skipTextBox1) TextBox2.Focus();
 }

But this code doesn't work. After that I found on MSDN:

Do not attempt to set focus from wit开发者_如何学Chin the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers.

So how can I do it other way?


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(); });
    }


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();
   }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜