Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a WinForm application with several input controls on a form. In the validation event handler (either Validating or Validated), I need to determine what control to activate next, based on the validated value.

In Microsoft's documentation of the Validating event, it states:

Caution

Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. Doing so can cause your application or the operating system to stop responding. For more information, see the WM_KILLFOCUS topic in the "Keyboard Input Reference" section, and the "Message Deadlocks" section of the "About Messages and Message Queues" topic in the MSDN library at http: // msdn.microsoft.com/library.

There is an ActiveControl property for a Form class that allows setting the control that is to become active, and no restrictions are mentioned. I have not found any other solution after several hours of web searches.

Is setting the ActiveControl property (instead of Focus) from my Validated event handler a safe way to positively activate the control I want? If not, are there any solutions?

Because the .NET Compact Framework doesn't have the ActiveControl property, can anyone suggest a solution?

like image 236
Suncat2000 Avatar asked Dec 16 '10 18:12

Suncat2000


People also ask

How to set focus on input field in ASP net?

To set focus on an ASP.NET Web server controlCall the control's Focus method. Call the page's SetFocus method, passing it the ID of the control on which you want to set focus. Security Note: This example has a text box that accepts user input, which is a potential security threat.

How to set focus on button in ASP net c#?

In ASP.NET 4.0, you can use two methods to set focus on controls. First, by using the control's Focus method. Second, calling page's SetFocus method and passing ID of the control in this SetFocus method. The following code snippet sets focus on TextBox1 by calling its Focus method on Page load event.

How do you control validation?

When the value of the CausesValidation property is set to true, you can also use the ValidationGroup property to specify the name of the validation group for which the Button control causes validation. Page has a Validate() method. If it is true this methods is executed. Validate() executes each validation control.

What is form validation control?

Before submitting data to the server, it is important to ensure all required form controls are filled out, in the correct format. This is called client-side form validation, and helps ensure data submitted matches the requirements set forth in the various form controls.


2 Answers

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;
    }
}
like image 40
Cody Gray Avatar answered Sep 21 '22 16:09

Cody Gray


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();
    }
like image 136
Hans Passant Avatar answered Sep 21 '22 16:09

Hans Passant