Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problems you're experiencing are a result of not reading the documentation carefully. The docs for the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx" rel="nofollow"><code>LostFocus</code> event</a> make very clear that you should <em>not</em> attempt to set the focus (e.g., by calling the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx" rel="nofollow"><code>Focus</code> method</a>) in the handler method for this event:</p> <blockquote> <p><strong>Caution</strong></p> <p>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 <code>WM_KILLFOCUS</code> 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 <a href="http://msdn.microsoft.com/library" rel="nofollow">http://msdn.microsoft.com/library</a>.</p> </blockquote> <p>A better solution is to handle the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx" rel="nofollow"><code>Validating</code> event</a>. You'll perform your input validating inside the <code>Validating</code> event handler method, which is raised automatically by the .NET Framework.</p> <p>If the input is valid, you do nothing. If the input is <em>not</em> valid, you set the <code>Cancel</code> property of the <code>CancelEventArgs</code> parameter to <code>true</code>, causing all of the downstream events to be suppressed.</p> <pre><code>Private Sub myTextBox_Validating(ByVal sender As Object, _ ByVal e As ComponentModel.CancelEventArgs) _ Handles myTextBox.Validating If Not Valid(myTextBox.Text) Then ' Validation failed, so cancel the event and pre-select the text. e.Cancel = True myTextBox.Select(0, myTextBox.Text.Length) ' Optionally (but recommended): Set an ErrorProvider control. ' errorProvider.SetError(myTextBox, "Invalid input") End If End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload