Export (0) Print
Expand All
Code: Changing the Background Color of a Windows Form (Visual Basic)
Code: Copying a Bitmap Image to the Clipboard (Visual Basic)
Code: Creating a Context Menu and Attaching it to a Windows Forms Control (Visual Basic)
Code: Determining Which Control on a Windows Form is Selected (Visual Basic)
Code: Displaying a Web Page in Internet Explorer (Visual Basic)
Code: Displaying an OpenFileDialog Component Dynamically (Visual Basic)
Code: Displaying One Windows Form from Another (Visual Basic)
Code: Displaying Print Preview for a Windows Form (Visual Basic)
Code: Drawing Graphics on a Windows Form (Visual Basic)
Code: Getting a Value from Another Windows Form (Visual Basic)
Code: Hosting a Windows Forms Control in Internet Explorer
Code: Printing a DataGrid (Visual Basic)
Code: Printing a Text File (Visual Basic)
Code: Retrieving a Bitmap Image From the Clipboard (Visual Basic)
Code: Validating User Input on Windows Forms (Visual Basic)
Expand Minimize
This topic has not yet been rated - Rate this topic

Code: Overriding Control Validation on Windows Forms in the Closing Event (Visual Basic)

Visual Studio .NET 2003

This example shows how to close a form even if there is validation logic in the form and one or more of the controls contain invalid data. In the example, there is validation logic for a TextBox control. Validation is overridden in the form's Closing event.

Example

Private Sub TextBox1_Validating(ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs) _ 
    Handles TextBox1.Validating
    If TextBox1.Text.Length < 8 Then
            ("Value you entered is not valid;" _
            & " it must be at least 8 characters.")
        e.Cancel = True
    End If
End Sub

Private Sub Form1_Closing(ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs) _ 
    Handles MyBase.Closing
    e.Cancel = False
End Sub

Compiling the Code

This project requires:

  • A reference to the System namespace.
  • A Windows Form.
  • A TextBox control named TextBox1.
  • Another control on the form that can receive focus to illustrate validation being overridden.

Security

User input into a Windows Forms can include potentially invalid, malformed, or malicious information. Always check that the values that a user enters conform to the requirements for your application before using the values.

See Also

Code: Validating User Input on Windows Forms (Visual Basic) | Validation of Control Data on Windows Forms | Form.Closing Event | CancelEventArgs Class | Windows Applications Example Topics

Show:
© 2014 Microsoft. All rights reserved.