VB.Net Tutorial/GUI/TextBox Validation
Версия от 16:40, 26 мая 2010; (обсуждение)
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.ruponentModel.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.ruponentModel.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
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.ruponentModel.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.ruponents = New System.ruponentModel.Container()
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.ruponents)
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.ruponentModel.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
Validate user information using regular expressions.
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
public class RegxValidateTextBox
public Shared Sub Main
Application.Run(New FrmValid)
End Sub
End class
Public Class FrmValid
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 txtState As System.Windows.Forms.TextBox
Friend WithEvents txtLast As System.Windows.Forms.TextBox
Friend WithEvents txtFirst As System.Windows.Forms.TextBox
Friend WithEvents lblZip As System.Windows.Forms.Label
Friend WithEvents lblPhone As System.Windows.Forms.Label
Friend WithEvents lblLast As System.Windows.Forms.Label
Friend WithEvents lblFirst As System.Windows.Forms.Label
Friend WithEvents lblAddress As System.Windows.Forms.Label
Friend WithEvents txtAddress As System.Windows.Forms.TextBox
Friend WithEvents txtZip As System.Windows.Forms.TextBox
Friend WithEvents txtCity As System.Windows.Forms.TextBox
Friend WithEvents lblCity As System.Windows.Forms.Label
Friend WithEvents lblState As System.Windows.Forms.Label
Friend WithEvents txtPhone As System.Windows.Forms.TextBox
Friend WithEvents cmdOK As System.Windows.Forms.Button
"Required by the Windows Form Designer
Private components As System.ruponentModel.Container
"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.txtState = New System.Windows.Forms.TextBox()
Me.txtLast = New System.Windows.Forms.TextBox()
Me.txtFirst = New System.Windows.Forms.TextBox()
Me.lblZip = New System.Windows.Forms.Label()
Me.lblPhone = New System.Windows.Forms.Label()
Me.lblLast = New System.Windows.Forms.Label()
Me.lblFirst = New System.Windows.Forms.Label()
Me.lblAddress = New System.Windows.Forms.Label()
Me.txtAddress = New System.Windows.Forms.TextBox()
Me.txtZip = New System.Windows.Forms.TextBox()
Me.txtCity = New System.Windows.Forms.TextBox()
Me.lblCity = New System.Windows.Forms.Label()
Me.lblState = New System.Windows.Forms.Label()
Me.txtPhone = New System.Windows.Forms.TextBox()
Me.cmdOK = New System.Windows.Forms.Button()
Me.SuspendLayout()
"
"txtState
"
Me.txtState.Location = New System.Drawing.Point(100, 170)
Me.txtState.Name = "txtState"
Me.txtState.Size = New System.Drawing.Size(136, 20)
Me.txtState.TabIndex = 4
Me.txtState.Text = ""
"
"txtLast
"
Me.txtLast.Location = New System.Drawing.Point(100, 30)
Me.txtLast.Name = "txtLast"
Me.txtLast.Size = New System.Drawing.Size(136, 20)
Me.txtLast.TabIndex = 0
Me.txtLast.Text = ""
"
"txtFirst
"
Me.txtFirst.Location = New System.Drawing.Point(100, 65)
Me.txtFirst.Name = "txtFirst"
Me.txtFirst.Size = New System.Drawing.Size(136, 20)
Me.txtFirst.TabIndex = 1
Me.txtFirst.Text = ""
"
"lblZip
"
Me.lblZip.Location = New System.Drawing.Point(10, 205)
Me.lblZip.Name = "lblZip"
Me.lblZip.Size = New System.Drawing.Size(80, 24)
Me.lblZip.TabIndex = 13
Me.lblZip.Text = "Zip"
Me.lblZip.TextAlign = System.Drawing.ContentAlignment.MiddleRight
"
"lblPhone
"
Me.lblPhone.Location = New System.Drawing.Point(10, 240)
Me.lblPhone.Name = "lblPhone"
Me.lblPhone.Size = New System.Drawing.Size(80, 24)
Me.lblPhone.TabIndex = 14
Me.lblPhone.Text = "Phone"
Me.lblPhone.TextAlign = System.Drawing.ContentAlignment.MiddleRight
"
"lblLast
"
Me.lblLast.Location = New System.Drawing.Point(10, 30)
Me.lblLast.Name = "lblLast"
Me.lblLast.Size = New System.Drawing.Size(80, 24)
Me.lblLast.TabIndex = 8
Me.lblLast.Text = "Last Name"
Me.lblLast.TextAlign = System.Drawing.ContentAlignment.MiddleRight
"
"lblFirst
"
Me.lblFirst.Location = New System.Drawing.Point(10, 65)
Me.lblFirst.Name = "lblFirst"
Me.lblFirst.Size = New System.Drawing.Size(80, 24)
Me.lblFirst.TabIndex = 9
Me.lblFirst.Text = "First Name"
Me.lblFirst.TextAlign = System.Drawing.ContentAlignment.MiddleRight
"
"lblAddress
"
Me.lblAddress.Location = New System.Drawing.Point(10, 100)
Me.lblAddress.Name = "lblAddress"
Me.lblAddress.Size = New System.Drawing.Size(80, 24)
Me.lblAddress.TabIndex = 10
Me.lblAddress.Text = "Address"
Me.lblAddress.TextAlign = System.Drawing.ContentAlignment.MiddleRight
"
"txtAddress
"
Me.txtAddress.Location = New System.Drawing.Point(100, 100)
Me.txtAddress.Name = "txtAddress"
Me.txtAddress.Size = New System.Drawing.Size(136, 20)
Me.txtAddress.TabIndex = 2
Me.txtAddress.Text = ""
"
"txtZip
"
Me.txtZip.Location = New System.Drawing.Point(100, 205)
Me.txtZip.Name = "txtZip"
Me.txtZip.Size = New System.Drawing.Size(136, 20)
Me.txtZip.TabIndex = 5
Me.txtZip.Text = ""
"
"txtCity
"
Me.txtCity.Location = New System.Drawing.Point(100, 135)
Me.txtCity.Name = "txtCity"
Me.txtCity.Size = New System.Drawing.Size(136, 20)
Me.txtCity.TabIndex = 3
Me.txtCity.Text = ""
"
"lblCity
"
Me.lblCity.Location = New System.Drawing.Point(10, 135)
Me.lblCity.Name = "lblCity"
Me.lblCity.Size = New System.Drawing.Size(80, 24)
Me.lblCity.TabIndex = 11
Me.lblCity.Text = "City"
Me.lblCity.TextAlign = System.Drawing.ContentAlignment.MiddleRight
"
"lblState
"
Me.lblState.Location = New System.Drawing.Point(10, 170)
Me.lblState.Name = "lblState"
Me.lblState.Size = New System.Drawing.Size(80, 24)
Me.lblState.TabIndex = 12
Me.lblState.Text = "State"
Me.lblState.TextAlign = System.Drawing.ContentAlignment.MiddleRight
"
"txtPhone
"
Me.txtPhone.Location = New System.Drawing.Point(100, 240)
Me.txtPhone.Name = "txtPhone"
Me.txtPhone.Size = New System.Drawing.Size(136, 20)
Me.txtPhone.TabIndex = 6
Me.txtPhone.Text = ""
"
"cmdOK
"
Me.cmdOK.Location = New System.Drawing.Point(112, 296)
Me.cmdOK.Name = "cmdOK"
Me.cmdOK.TabIndex = 15
Me.cmdOK.Text = "OK"
"
"FrmValid
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(312, 341)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblPhone, Me.lblZip, Me.lblState, Me.lblCity, Me.lblAddress, Me.lblFirst, Me.lblLast, Me.cmdOK, Me.txtPhone, Me.txtZip, Me.txtState, Me.txtCity, Me.txtAddress, Me.txtFirst, Me.txtLast})
Me.Name = "FrmValid"
Me.Text = "Validate"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub cmdOK_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdOK.Click
If Not Regex.Match(txtLast.Text, "^[A-Z][a-zA-Z]*$").Success Then
txtLast.Text = "Invalid Last Name"
txtLast.Focus()
Return
End If
If Not Regex.Match(txtFirst.Text, "^[A-Z][a-zA-Z]*$").Success Then
txtFirst.Text = "Invalid First Name"
txtFirst.Focus()
Return
End If
If Not Regex.Match(txtAddress.Text, "^[0-9]+\s+([a-zA-Z]" & "+|[a-zA-Z]+\s[a-zA-Z]+)$").Success Then
txtAddress.Text = "Invalid Address"
txtAddress.Focus()
Return
End If
If Not Regex.Match(txtCity.Text, "^([a-zA-Z]+|[a-zA-Z]" & "+\s[a-zA-Z]+)$").Success Then
txtCity.Text = "Invalid City"
txtCity.Focus()
Return
End If
If Not Regex.Match(txtState.Text,"^([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)$").Success Then
txtState.Text = "Invalid State"
txtState.Focus()
Return
End If
If Not Regex.Match(txtZip.Text, "^\d{5}$").Success Then
txtZip.Text = "Invalid zip code"
txtZip.Focus()
Return
End If
If Not Regex.Match(txtPhone.Text, "^[1-9]" & "\d{2}-[1-9]\d{2}-\d{4}$").Success Then
txtPhone.Text = "Invalid Phone Number"
txtPhone.Focus()
Return
End If
Me.Hide()
Application.Exit()
End Sub " cmdOK_Click
End Class