TextField validation: cannot be empty : TextBox Validation « GUI « VB.Net Tutorial






TextField validation: cannot be empty
Imports System.Text.RegularExpressions

Imports System.Windows.Forms

public class AddressValidationNotEmpty
   public Shared Sub Main
        Application.Run(New Address)
   End Sub
End class

Public Class Address
  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
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents Label4 As System.Windows.Forms.Label
  Friend WithEvents Label5 As System.Windows.Forms.Label
  Friend WithEvents lblStreet As System.Windows.Forms.Label
  Friend WithEvents txtStreet As System.Windows.Forms.TextBox
  Friend WithEvents txtState As System.Windows.Forms.TextBox
  Friend WithEvents txtCountry As System.Windows.Forms.TextBox
  Friend WithEvents lblCity As System.Windows.Forms.Label
  Friend WithEvents btnCancel As System.Windows.Forms.Button
  Friend WithEvents txtZip As System.Windows.Forms.TextBox
  Friend WithEvents txtCity As System.Windows.Forms.TextBox
  Friend WithEvents btnOK As System.Windows.Forms.Button
  Friend WithEvents errAddress As System.Windows.Forms.ErrorProvider
  Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
  Private components As System.ComponentModel.IContainer

  'Required by the Windows Form Designer

  '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.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.components = New System.ComponentModel.Container()
    Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
    Me.txtCountry = New System.Windows.Forms.TextBox()
    Me.Label4 = New System.Windows.Forms.Label()
    Me.Label5 = New System.Windows.Forms.Label()
    Me.txtCity = New System.Windows.Forms.TextBox()
    Me.Label3 = New System.Windows.Forms.Label()
    Me.txtZip = New System.Windows.Forms.TextBox()
    Me.btnOK = New System.Windows.Forms.Button()
    Me.lblCity = New System.Windows.Forms.Label()
    Me.btnCancel = New System.Windows.Forms.Button()
    Me.txtState = New System.Windows.Forms.TextBox()
    Me.txtStreet = New System.Windows.Forms.TextBox()
    Me.errAddress = New System.Windows.Forms.ErrorProvider()
    Me.lblStreet = New System.Windows.Forms.Label()
    Me.SuspendLayout()
    '
    'txtCountry
    '
    Me.txtCountry.Location = New System.Drawing.Point(88, 64)
    Me.txtCountry.Name = "txtCountry"
    Me.txtCountry.Size = New System.Drawing.Size(112, 20)
    Me.txtCountry.TabIndex = 4
    Me.txtCountry.Text = "txtCountry"
    '
    'Label4
    '
    Me.Label4.AutoSize = True
    Me.Label4.CausesValidation = False
    Me.Label4.Location = New System.Drawing.Point(208, 68)
    Me.Label4.Name = "Label4"
    Me.Label4.Size = New System.Drawing.Size(42, 13)
    Me.Label4.TabIndex = 10
    Me.Label4.Text = "Zip/PC:"
    '
    'Label5
    '
    Me.Label5.AutoSize = True
    Me.Label5.CausesValidation = False
    Me.Label5.Location = New System.Drawing.Point(232, 44)
    Me.Label5.Name = "Label5"
    Me.Label5.Size = New System.Drawing.Size(60, 13)
    Me.Label5.TabIndex = 11
    Me.Label5.Text = "State/Prov:"
    '
    'txtCity
    '
    Me.txtCity.Location = New System.Drawing.Point(88, 40)
    Me.txtCity.Name = "txtCity"
    Me.txtCity.Size = New System.Drawing.Size(136, 20)
    Me.txtCity.TabIndex = 2
    Me.txtCity.Text = "txtCity"
    '
    'Label3
    '
    Me.Label3.AutoSize = True
    Me.Label3.CausesValidation = False
    Me.Label3.Location = New System.Drawing.Point(8, 68)
    Me.Label3.Name = "Label3"
    Me.Label3.Size = New System.Drawing.Size(47, 13)
    Me.Label3.TabIndex = 9
    Me.Label3.Text = "Country:"
    '
    'txtZip
    '
    Me.txtZip.Location = New System.Drawing.Point(264, 64)
    Me.txtZip.Name = "txtZip"
    Me.txtZip.Size = New System.Drawing.Size(96, 20)
    Me.txtZip.TabIndex = 1
    Me.txtZip.Text = "txtZip"
    '
    'btnOK
    '
    Me.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK
    Me.btnOK.Location = New System.Drawing.Point(200, 96)
    Me.btnOK.Name = "btnOK"
    Me.btnOK.TabIndex = 5
    Me.btnOK.Text = "OK"
    '
    'lblCity
    '
    Me.lblCity.AutoSize = True
    Me.lblCity.CausesValidation = False
    Me.lblCity.Location = New System.Drawing.Point(8, 44)
    Me.lblCity.Name = "lblCity"
    Me.lblCity.Size = New System.Drawing.Size(27, 13)
    Me.lblCity.TabIndex = 8
    Me.lblCity.Text = "City:"
    '
    'btnCancel
    '
    Me.btnCancel.CausesValidation = False
    Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
    Me.btnCancel.Location = New System.Drawing.Point(285, 96)
    Me.btnCancel.Name = "btnCancel"
    Me.btnCancel.TabIndex = 6
    Me.btnCancel.Text = "Cancel"
    '
    'txtState
    '
    Me.txtState.Location = New System.Drawing.Point(296, 40)
    Me.txtState.Name = "txtState"
    Me.txtState.Size = New System.Drawing.Size(64, 20)
    Me.txtState.TabIndex = 3
    Me.txtState.Text = "txtState"
    '
    'txtStreet
    '
    Me.txtStreet.Location = New System.Drawing.Point(88, 16)
    Me.txtStreet.Name = "txtStreet"
    Me.txtStreet.Size = New System.Drawing.Size(272, 20)
    Me.txtStreet.TabIndex = 0
    Me.txtStreet.Text = "txtStreet"
    '
    'errAddress
    '
    Me.errAddress.DataMember = Nothing
    '
    'lblStreet
    '
    Me.lblStreet.AutoSize = True
    Me.lblStreet.CausesValidation = False
    Me.lblStreet.Location = New System.Drawing.Point(8, 20)
    Me.lblStreet.Name = "lblStreet"
    Me.lblStreet.Size = New System.Drawing.Size(38, 13)
    Me.lblStreet.TabIndex = 7
    Me.lblStreet.Text = "Street:"
    '
    'Address
    '
    Me.AcceptButton = Me.btnOK
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.CancelButton = Me.btnCancel
    Me.ClientSize = New System.Drawing.Size(378, 137)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label5, Me.Label4, Me.Label3, Me.lblCity, Me.lblStreet, Me.btnCancel, Me.btnOK, Me.txtCountry, Me.txtState, Me.txtCity, Me.txtZip, Me.txtStreet})
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
    Me.MaximizeBox = False
    Me.MinimizeBox = False
    Me.Name = "Address"
    Me.Text = "Address"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Private Sub GenericNotEmpty(ByVal sender As Object, _
  ByVal e As System.ComponentModel.CancelEventArgs) _
  Handles txtStreet.Validating, txtCity.Validating, _
    txtState.Validating

    Dim ctlSender As Control
    ctlSender = CType(sender, Control)
    If ctlSender.Text.Trim = "" Then
      e.Cancel = True
      errAddress.SetError(ctlSender, "Must not be empty")
    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.