Alternative to set focus from within the Enter event

前端 未结 3 1084
栀梦
栀梦 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(); });
        }
    

提交回复
热议问题