How to set focus to a control after validation in .NET

后端 未结 4 1470
既然无缘
既然无缘 2021-01-18 12:12

I have a WinForm application with several input controls on a form. In the validation event handler (either Validating or Validated), I need to det

相关标签:
4条回答
  • 2021-01-18 12:33

    Yes, changing the focus during a Validating event is quite troublesome. The event is raised at the exact time the focus changes. The next control has already obtained the focus as far as Windows is concerned but the logical form state still has the focus at the control being validated. When you set e.Cancel to true, Winforms must undo the Windows focus state. When you don't, it must update the logical state after the event. There are a wide variety of things that can go wrong when you change focus yourself.

    It is important that you wait until the focus has been sorted out. Best thing to do is to delay your code until everything is done running and the form goes idle again. You can cleanly do so by using the Control.BeginInvoke() method. Something like this:

        private delegate void ChangeFocusDelegate(Control ctl);
    
        private void textBox1_Validating(object sender, CancelEventArgs e) {
            int value;
            if (!int.TryParse(textBox1.Text, out value)) e.Cancel = true;
            else {
                if (value == 1) this.BeginInvoke(new ChangeFocusDelegate(changeFocus), textBox2);
                else this.BeginInvoke(new ChangeFocusDelegate(changeFocus), textBox3);
            }
        }
        private void changeFocus(Control ctl) {
            ctl.Focus();
        }
    
    0 讨论(0)
  • 2021-01-18 12:33

    Form.SelectNextControl or Control.SelectNextControl

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.selectnextcontrol.aspx

    0 讨论(0)
  • 2021-01-18 12:46

    Have you tried setting the Cancel property of the CancelEventArgs that are passed to the Validating event handler to False?

    This is the intended way to keep the focus on the current control and prevent the next control from getting focus if validation fails. For example:

    private void TextBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        //Make sure that the textbox is not left blank
        if (string.IsNullOrEmpty(TextBox1.Text))
        {
            e.Cancel = true;
        }
    }
    
    0 讨论(0)
  • 2021-01-18 12:46

    This thread is old, but I have a couple of thoughts:

    1. Every control has a Tag property. What about giving the control you want have focus a unique Tag value, and then create a method that will iterate through the controls to find that control? Then you can set focus to it.

    2. Instead of using the Validating event, why not use Leaving instead? Doesn't seem to have the same quirks.

    0 讨论(0)
提交回复
热议问题