⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textbox.vb

📁 textbox information try to validation of your text box
💻 VB
字号:
'-----------------------------'-------------------------------------------------
'Author   : Moyed Sidhpurwala
'Date     : 06-October-2004
'Comments : Purpose of creating this control is to eliminate
'           validation code in actual forms which makes code look like turmoil 
'-----------------------------'--------------------------------------------------
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Resources

''' -----------------------------------------------------------------------------
''' Project	 : glblControls
''' Class	 : TextBox
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Class Textbox got Properties, Method and Events to handle Regular expressions
''' </summary>
''' <remarks>
''' Textbox Class Purpose of creating this control is to eliminate validation code in
''' actual forms which makes code look like turmoil 
''' </remarks>
''' <history>
''' 	[mms]	11/30/2004	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class TextBox
    Inherits System.Windows.Forms.TextBox

    Private firstTime As Boolean = True
    Private firstLoad As Boolean = True
    Private secondLoad As Boolean = False
    Private thirdLoad As Boolean = False
    Private lastText As String = ""
    Private keyType As String
    Private isRequired As Byte = 0
    Private vexit As Boolean = False
    Private WithEvents miForm As Form = MyBase.FindForm
    Private LETTERS As String = "A罛CDE蒄GHI蚃KLMN袿覲QRSTU谲VWXYZ"
    Private NUMBERS As String = "1234567890"

#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

    'UserControl1 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 erpShowError As System.Windows.Forms.ErrorProvider
    Friend WithEvents erpShowRequired As System.Windows.Forms.ErrorProvider
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(TextBox))
        Me.erpShowError = New System.Windows.Forms.ErrorProvider
        Me.erpShowRequired = New System.Windows.Forms.ErrorProvider
        '
        'erpShowError
        '
        Me.erpShowError.Icon = CType(resources.GetObject("erpShowError.Icon"), System.Drawing.Icon)
        '
        'TextBox
        '

    End Sub

#End Region

#Region " Attributes "

#Region " Fields "

    Private m_ShowErrorIcon As Boolean
    Private m_TextBox As String
    Private m_Required As Boolean
    Private m_ValidationMode As ValidationModes

#End Region 'Fields

#Region " Properties "

    'COMMENT: Sets if error icon is showed.
    <Description("Sets if error icon is showed."), Category("Validation")> _
    Public Property ShowErrorIcon() As Boolean
        Get
            Return m_ShowErrorIcon
        End Get
        Set(ByVal Value As Boolean)
            m_ShowErrorIcon = Value
        End Set
    End Property 'ShowErrorIcon

    <Description("TextBox to use in validation."), Category("Validation")> _
    Public Property TextBox() As String
        Get
            Return m_TextBox
        End Get
        Set(ByVal Value As String)
            m_TextBox = Value
        End Set
    End Property 'TextBox

    'COMMENT: Sets the field as required for validation.
    <Description("Sets the field as required for validation."), Category("Validation")> _
    Public Property Required() As Boolean
        Get
            Return m_Required
        End Get
        Set(ByVal Value As Boolean)
            m_Required = Value
            If m_Required = True Then
                Me.CausesValidation = True
                Me.BackColor = System.Drawing.Color.OldLace
            Else
                Me.BackColor = System.Drawing.Color.White
            End If
        End Set
    End Property 'Required

    'COMMENT: Validation Modes
    Enum ValidationModes
        None = 0
        ValidCharacters = 1
        InvalidCharacters = 2
        Letters = 3
        Numbers = 4
    End Enum 'ValidationMode

    'COMMENT: Sets validation mode of TextBox control.
    <Description("Sets the validation mode to use."), Category("Validation"), RefreshProperties(RefreshProperties.All)> _
    Public Property ValidationMode() As ValidationModes
        Get
            Return m_ValidationMode
        End Get

        Set(ByVal Value As ValidationModes)
            m_ValidationMode = Value

            If m_ValidationMode = ValidationModes.None Then
                Me.m_TextBox = ""
            End If

        End Set

    End Property 'ValidationMode

#End Region 'Properties

#End Region 'Attributes

#Region " Methods "

    'COMMENT: Character entry validation.
    Private Sub eventTextChanged_TextBox(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.TextChanged
        If vexit = True Then
            vexit = False
            Exit Sub
        End If
        If secondLoad = True Then
            Me.lastText = Me.Text
            secondLoad = False
            thirdLoad = True
        End If
        If firstLoad = True Then
            Me.secondLoad = True
            firstLoad = False
        End If
        Me.erpShowRequired.SetError(Me, "")
        If Me.m_ValidationMode = ValidationModes.None Then
            Me.erpShowError.SetError(Me, "")
        End If

        Dim i As Integer
        Dim j As Integer
        Dim check As Boolean = False
        Dim valid As Boolean = True
        Dim InvalidCharacters As New Collection
        Select Case m_ValidationMode
            Case ValidationModes.ValidCharacters 'Caracteres valids
                '---------------------------
                If firstTime = True Then
                    Dim validString As String = Me.m_TextBox
                    check = False
                    If Not Me.Text = "" And Not validString = "" Then
                        firstTime = False
                        Dim lastCharacter As String = Mid(Me.Text, Me.Text.Length, 1)
                        For i = 0 To Me.Text.Length - 1
                            For j = 0 To validString.Length - 1
                                If Me.Text.Chars(i) = validString.Chars(j) Then
                                    check = True
                                End If
                            Next
                            If j = validString.Length And check = False Then
                                valid = False
                                InvalidCharacters.Add(Me.Text.Chars(i))
                            End If
                            check = False
                        Next
                        If valid = False Then
                            If Me.m_ShowErrorIcon = True Then
                                showError(InvalidCharacters)
                            End If
                            Me.Text = lastText
                            Me.SelectionStart = Me.Text.Length
                        Else
                            Me.erpShowError.SetError(Me, "")
                        End If
                        firstTime = True
                    Else
                        Me.erpShowError.SetError(Me, "")
                    End If
                    lastText = Me.Text
                End If
                '---------------------------

            Case ValidationModes.InvalidCharacters 'Caracteres invalids
                '---------------------------
                If firstTime = True Then
                    Dim validString As String = Me.m_TextBox
                    check = False
                    If Not Me.Text = "" And Not validString = "" Then
                        firstTime = False
                        Dim lastCharacter As String = Mid(Me.Text, Me.Text.Length, 1)
                        For i = 0 To Me.Text.Length - 1
                            For j = 0 To validString.Length - 1
                                If Me.Text.Chars(i) = validString.Chars(j) Then
                                    check = True
                                End If
                            Next
                            If j = validString.Length And check = True Then
                                valid = False
                                InvalidCharacters.Add(Me.Text.Chars(i))
                            End If
                            check = False
                        Next
                        If valid = False Then
                            If Me.m_ShowErrorIcon = True Then
                                showError(InvalidCharacters)
                            End If
                            Me.Text = lastText
                            Me.SelectionStart = Me.Text.Length
                        Else
                            Me.erpShowError.SetError(Me, "")
                        End If
                        firstTime = True
                    Else
                        Me.erpShowError.SetError(Me, "")
                    End If
                    lastText = Me.Text
                End If
                '---------------------------

            Case ValidationModes.Letters 'LETTERS
                '---------------------------
                If firstTime = True Then
                    Dim validString As String = "A罛CDE蒄GHI蚃KLMN袿覲QRSTU谲VWXYZa醔cde閒ghi韏klmn駉髉qrstu

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -