IP地址输入控件 V1.0.1.1版(For Win From)

Public Class YujiaIPCheckInput
    Inherits System.Windows.Forms.UserControl
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        InitializeComponent()


        aryRanges(0, 1) = 255
        aryRanges(1, 1) = 255
        aryRanges(2, 1) = 255
        aryRanges(3, 1) = 255

        If Not Me.DesignMode Then ClearAddress()
    End Sub


    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

    Private components As System.ComponentModel.IContainer


    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents EP As System.Windows.Forms.ErrorProvider
    Private WithEvents txtField0 As System.Windows.Forms.TextBox
    Private WithEvents txtField1 As System.Windows.Forms.TextBox
    Private WithEvents txtField2 As System.Windows.Forms.TextBox
    Private WithEvents txtField3 As System.Windows.Forms.TextBox
    Private Sub InitializeComponent()
        Me.txtField0 = New System.Windows.Forms.TextBox
        Me.txtField1 = New System.Windows.Forms.TextBox
        Me.txtField2 = New System.Windows.Forms.TextBox
        Me.txtField3 = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.EP = New System.Windows.Forms.ErrorProvider
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'txtField0
        '
        Me.txtField0.AutoSize = False
        Me.txtField0.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtField0.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtField0.MaxLength = 3
        Me.txtField0.Name = "txtField0"
        Me.txtField0.Size = New System.Drawing.Size(24, 18)
        Me.txtField0.TabIndex = 0
        Me.txtField0.Text = "888"
        '
        'txtField1
        '
        Me.txtField1.AutoSize = False
        Me.txtField1.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtField1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtField1.Location = New System.Drawing.Point(28, 0)
        Me.txtField1.MaxLength = 3
        Me.txtField1.Name = "txtField1"
        Me.txtField1.Size = New System.Drawing.Size(24, 18)
        Me.txtField1.TabIndex = 1
        Me.txtField1.Text = "888"
        '
        'txtField2
        '
        Me.txtField2.AutoSize = False
        Me.txtField2.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtField2.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtField2.Location = New System.Drawing.Point(56, 0)
        Me.txtField2.MaxLength = 3
        Me.txtField2.Name = "txtField2"
        Me.txtField2.Size = New System.Drawing.Size(24, 18)
        Me.txtField2.TabIndex = 2
        Me.txtField2.Text = "888"
        '
        'txtField3
        '
        Me.txtField3.AutoSize = False
        Me.txtField3.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtField3.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.EP.SetIconAlignment(Me.txtField3, System.Windows.Forms.ErrorIconAlignment.MiddleLeft)
        Me.txtField3.Location = New System.Drawing.Point(84, 0)
        Me.txtField3.MaxLength = 3
        Me.txtField3.Name = "txtField3"
        Me.txtField3.Size = New System.Drawing.Size(24, 18)
        Me.txtField3.TabIndex = 3
        Me.txtField3.Text = "888"
        '
        'Label1
        '
        Me.Label1.BackColor = System.Drawing.SystemColors.Window
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(24, 0)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(4, 18)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "."
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Panel1
        '
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3, Me.Label2, Me.txtField3, Me.Label1, Me.txtField0, Me.txtField1, Me.txtField2})
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(112, 21)
        Me.Panel1.TabIndex = 5
        '
        'Label3
        '
        Me.Label3.BackColor = System.Drawing.SystemColors.Window
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.Location = New System.Drawing.Point(80, 0)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(4, 18)
        Me.Label3.TabIndex = 3
        Me.Label3.Text = "."
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label2
        '
        Me.Label2.BackColor = System.Drawing.SystemColors.Window
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(52, 0)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(4, 18)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "."
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'ctlIPAddress
        '
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1})
        Me.Name = "ctlIPAddress"
        Me.Size = New System.Drawing.Size(112, 21)
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Enum IPFields
        Field0 = 0
        Field1 = 1
        Field2 = 2
        Field3 = 3
    End Enum

    Dim aryRanges(3, 1) As Byte

    '只允许输入数字、"."
    Private Sub txtField_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtField0.KeyPress, txtField1.KeyPress, txtField2.KeyPress, txtField3.KeyPress
        If e.KeyChar = "." Then
            Dim tx As TextBox = CType(sender, TextBox)
            '最后的文本框不可以输入"."
            '在其它文本框输入".",相当于输入"Tab"键,即光标到下一文本框
            If tx.Text = "" Or tx.SelectedText <> "" Then
                e.Handled = True
            End If
            If tx.Name <> "txtField3" Then
                SendKeys.Send("{TAB}")
            Else
                '最后的文本框不可以输入"."
                e.Handled = True
            End If
        ElseIf (Not IsNumeric(e.KeyChar)) And e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        Else
            Dim tx As TextBox = CType(sender, TextBox)
            If (Len(tx.Text) = 2 And e.KeyChar <> ControlChars.Back And tx.SelectedText.Length = 0) Then
                If tx.Name <> "txtField3" Then
                    SendKeys.Send("{TAB}")
                End If
            End If
        End If
        '控制ControlChars.Back退格键
        Dim tx1 As TextBox = CType(sender, TextBox)
        If (Len(tx1.Text) = 0 And e.KeyChar = ControlChars.Back) Then
            Select Case tx1.Name
                Case "txtField3"
                    If txtField3.Text = "" Then
                        txtField2.Focus()
                    End If
                Case "txtField2"
                    If txtField2.Text = "" Then
                        txtField1.Focus()
                    End If
                Case "txtField1"
                    If txtField1.Text = "" Then
                        txtField0.Focus()
                    End If
            End Select
        End If
    End Sub

    'txtField0
    Private Sub txtField0_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtField0.Validating

        Dim intVal As Integer

        If txtField0.Text <> "" Then intVal = CInt(txtField0.Text)

        '检验是否在000-255之间
        If intVal < aryRanges(0, 0) Or intVal > aryRanges(0, 1) Then
            EP.SetError(txtField0, "IP地址输入有误,必须在 " & aryRanges(0, 0) & " 至 " & aryRanges(0, 1) & "之间!")
            e.Cancel = True
        Else
            txtField0.Text = intVal
        End If
    End Sub

    Private Sub txtField0_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtField0.Validated
        EP.SetError(txtField0, "")
    End Sub

    'txtField1
    Private Sub txtField1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtField1.Validating

        Dim intVal As Integer

        If txtField1.Text <> "" Then intVal = CInt(txtField1.Text)


        If intVal < aryRanges(1, 0) Or intVal > aryRanges(1, 1) Then
            EP.SetError(txtField1, "IP地址输入有误,必须在 " & aryRanges(0, 0) & " 至 " & aryRanges(0, 1) & "之间!")
            e.Cancel = True
        Else
            txtField1.Text = intVal
        End If
    End Sub

    Private Sub txtField1_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtField1.Validated
        EP.SetError(txtField1, "")
    End Sub

    'txtField2
    Private Sub txtField2_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtField2.Validating

        Dim intVal As Integer

        If txtField2.Text <> "" Then intVal = CInt(txtField2.Text)


        If intVal < aryRanges(2, 0) Or intVal > aryRanges(2, 1) Then
            EP.SetError(txtField2, "IP地址输入有误,必须在 " & aryRanges(0, 0) & " 至 " & aryRanges(0, 1) & "之间!")
            e.Cancel = True
        Else
            txtField2.Text = intVal
        End If
    End Sub

    Private Sub txtField2_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtField2.Validated
        EP.SetError(txtField2, "")
    End Sub

    'txtField3
    Private Sub txtField3_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtField3.Validating

        Dim intVal As Integer

        If txtField3.Text <> "" Then intVal = CInt(txtField3.Text)


        If intVal < aryRanges(3, 0) Or intVal > aryRanges(3, 1) Then
            EP.SetError(txtField3, "IP地址输入有误,必须在 " & aryRanges(0, 0) & " 至 " & aryRanges(0, 1) & "之间!")
            e.Cancel = True
        Else
            txtField3.Text = intVal
        End If
    End Sub

    Private Sub txtField3_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtField3.Validated
        EP.SetError(txtField3, "")
    End Sub

    Public Sub ClearAddress()
        txtField0.Text = ""
        txtField1.Text = ""
        txtField2.Text = ""
        txtField3.Text = ""
    End Sub

    Public ReadOnly Property IsComplete() As Boolean
        Get
            If txtField0.Text <> "" And txtField1.Text <> "" And txtField2.Text <> "" And txtField3.Text <> "" And _
            EP.GetError(txtField0) = "" And EP.GetError(txtField1) = "" And EP.GetError(txtField2) = "" And EP.GetError(txtField3) = "" Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property

    Public Property MaxRange(ByVal Field As IPFields) As Byte
        Get
            If Field >= 0 And Field < 4 Then
                Return aryRanges(Field, 1)
            End If
        End Get
        Set(ByVal Value As Byte)
            If Field >= 0 And Field < 4 Then

                If Value >= aryRanges(Field, 0) Then
                    aryRanges(Field, 1) = Value
                End If
            End If
        End Set
    End Property

    Public Property MinRange(ByVal Field As IPFields) As Byte
        Get
            If Field >= 0 And Field < 4 Then
                Return aryRanges(Field, 0)
            End If
        End Get
        Set(ByVal Value As Byte)
            If Field >= 0 And Field < 4 Then

                If Value <= aryRanges(Field, 1) Then
                    aryRanges(Field, 0) = Value
                End If
            End If
        End Set
    End Property
    Public Function SelectAll()
        txtField0.SelectAll()
        txtField1.SelectAll()
        txtField2.SelectAll()
        txtField3.SelectAll()
    End Function
    Public Function ToShow(ByVal StrIP As String)
        Dim str As String()
        Try
            str = StrIP.Split(".")
            txtField0.Text = str(0)
            txtField1.Text = str(1)
            txtField2.Text = str(2)
            txtField3.Text = str(3)
        Catch ex As Exception
            txtField0.Text = 0
            txtField1.Text = 0
            txtField2.Text = 0
            txtField3.Text = 0
            MsgBox("IP地址错误,请重新输入!", MsgBoxStyle.OKOnly, "提示")
        End Try

    End Function
    Public Overrides Function ToString() As String
        If IsComplete Then
            Return txtField0.Text & "." & txtField1.Text & "." & txtField2.Text & "." & txtField3.Text
        Else
            Return ""
        End If
    End Function
End Class

以上为代码。
当运行时:

当出现错误时:
posted on 2004-02-17 14:16  郭岩  阅读(1664)  评论(3编辑  收藏  举报