traveller
traveller

Reputation: 189

Keep Focus on Textbox after user tried to move to other control (on failed validation) in winforms, .net 3.5 WEC7

I want to Validate Userinput when he continues to the next TextBox, and keep focus on the last editied TextBox, if the inmput is invalid. I tried the Validating and LostFocus events, but in both cases, if I try to refocus the TextBox, for which validation failed, the next textbox has already got focus, also throws the validating event... What I want:

User edits in TextBox A
User leaves TextBox A (Click on TextBox B or Tab or...)
Input in TextBox A is validated
If Validation fails, a MessageBox is shown
The focus stays on TextBox A

What happends:
User edits in TextBox A
User leaves TextBox A (Click on TextBox B or Tab or...)
Input in TextBox A is validated (in validating event)
If Validation fails, a MessageBox is shown
Setting the focus back to TextBox A fires vaidating event in Textbox B
There has been no input in TextBox B, so it is invalid
The Message, that content of TextBox B is invalid is shown
... Also, MSDN tells I should not set Focus in any of the following events: Enter, GotFocus, Leave, LostFocus, Validating, or Validated. So how can i return the focus to TextBox A, ifI must not set focus in one of these events?

        private void TextBoxA_Validating(object sender, System.ComponentModel.CancelEventArgs e) {            
        if (!IsValid(TextBoxA.Text)) // Some Method that returns false if Input is invalid
        {
            ... // show a message
                TextBoxA.Focus();
        }                         
    }

    private void TextBoxB_Validating(object sender, System.ComponentModel.CancelEventArgs e) {            
        if (!IsValid(TextBoxB.Text)) // Some Method that returns false if Input is invalid
        {
            ... // show a message
                TextBoxB.Focus();
        }                         
    }

Upvotes: 0

Views: 3737

Answers (2)

CodeTherapist
CodeTherapist

Reputation: 2806

You should use the select() function to activate the textbox, because Focus() is a low level function for user control developer, as noted at MSDN: Control.Focus()

You can use the CancelEventArgs to get the behavior you want:

private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{

   if(!ValidEmailAddress(textBox1.Text))
   {
      // Cancel the event and select the text to be corrected by the user.
      e.Cancel = true;
      textBox1.Select(0, textBox1.Text.Length);

      // Set the ErrorProvider error with the text to display.  
      this.errorProvider1.SetError(textBox1, "Validation error message goes here");
   }
   else
   {
       this.errorProvider1.SetError(textBox1, ""); // clears the error
   }
}

And last tip: use a error provider to show the validations to the user. The Messagebox is really uncomfortable for the user.

Upvotes: 0

r.net
r.net

Reputation: 631

I have not tested this, but don't use txtbox.Focus() if the validation fails. You should use e.Cancel set it to true. Give this a go and see if this fixes it.

Upvotes: 0

Related Questions