TextBox validating event : TextBox Validation « GUI « VB.Net Tutorial






TextBox validating event
Imports System.Windows.Forms

public class TextBoxValidating
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
   Friend WithEvents Label1 As System.Windows.Forms.Label
   Friend WithEvents txtInput As System.Windows.Forms.TextBox
   Friend WithEvents Label2 As System.Windows.Forms.Label
   Friend WithEvents Label3 As System.Windows.Forms.Label
   Friend WithEvents lblTrue As System.Windows.Forms.Label
   Friend WithEvents lblCheck As System.Windows.Forms.Label
   Friend WithEvents lblResults As System.Windows.Forms.Label
   Friend WithEvents btnClear As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.txtInput = New System.Windows.Forms.TextBox()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.lblTrue = New System.Windows.Forms.Label()
Me.lblCheck = New System.Windows.Forms.Label()
Me.lblResults = New System.Windows.Forms.Label()
Me.btnClear = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Tahoma", 14.25!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(48, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(176, 23)
Me.Label1.TabIndex = 0
Me.Label1.Text = "ISBN Validation"
'
'txtInput
'
Me.txtInput.Location = New System.Drawing.Point(72, 64)
Me.txtInput.Name = "txtInput"
Me.txtInput.TabIndex = 1
Me.txtInput.Text = ""
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(24, 104)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(80, 23)
Me.Label2.TabIndex = 2
Me.Label2.Text = "True Number:"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(32, 152)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(72, 23)
Me.Label3.TabIndex = 3
Me.Label3.Text = "Check Digit:"
'
'lblTrue
'
Me.lblTrue.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.lblTrue.Location = New System.Drawing.Point(112, 104)
Me.lblTrue.Name = "lblTrue"
Me.lblTrue.TabIndex = 4
'
'lblCheck
'
Me.lblCheck.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.lblCheck.Location = New System.Drawing.Point(112, 152)
Me.lblCheck.Name = "lblCheck"
Me.lblCheck.TabIndex = 5
'
'lblResults
'
Me.lblResults.Location = New System.Drawing.Point(56, 192)
Me.lblResults.Name = "lblResults"
Me.lblResults.Size = New System.Drawing.Size(152, 24)
Me.lblResults.TabIndex = 6
'
'btnClear
'
Me.btnClear.Location = New System.Drawing.Point(88, 240)
Me.btnClear.Name = "btnClear"
Me.btnClear.TabIndex = 7
Me.btnClear.Text = "Clear"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(264, 293)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnClear, Me.lblResults, Me.lblCheck, Me.lblTrue, Me.Label3, Me.Label2, Me.txtInput, Me.Label1})
Me.Name = "Form1"
Me.Text = "ISBN Validation"
Me.ResumeLayout(False)

    End Sub

#End Region

 
   Private Sub IsbnValidation(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtInput.Validating

         Console.WriteLine("validating")
   End Sub

   Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
      txtInput.Text = ""
      lblResults.Text = ""
      lblTrue.Text = ""
      lblCheck.Text = ""
   End Sub

   Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress
      Dim keyChar As Char
      keyChar = e.KeyChar
      
      ' Suppress any keys except digits,X,x,hyphen (45),Backspace (8),or Enter (13)
      If ((Not Char.IsDigit(keyChar)) _
         And (AscW(keyChar) <> 8) _
         And (AscW(keyChar) <> 13) _
         And (keyChar <> "X"c) _
         And (keyChar <> "x"c) _
         And (AscW(keyChar) <> 45)) Then
         '  Do not display the keystroke
         e.Handled = True
      End If
   End Sub
End Class








14.24.TextBox Validation
14.24.1.TextBox validating eventTextBox validating event
14.24.2.TextField validation: cannot be emptyTextField validation: cannot be empty
14.24.3.Validate user information using regular expressions.Validate user information using regular expressions.