VB.NET 输入IP地址的自定义用户控件

vb-net 专栏收录该内容
19 篇文章 0 订阅

VB2010的控件面板上有MaskedTextBox,可将其Mask属性设置为000.000.000.000,即可输入IP地址,但是实际使用中,总觉得与系统中网卡IP地址输入框相去甚远,因此采用四个文本框和四个标签控件(用于显示小数点)组合,制作一个类似的IP地址输入框控件。
新建一个类,命名为IpBox,代码如下:

Public Class IpBox
    Inherits System.Windows.Forms.UserControl

#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
    'UserControl 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 Panel1 As System.Windows.Forms.Panel
    Friend WithEvents textBox3 As System.Windows.Forms.TextBox
    Friend WithEvents label3 As System.Windows.Forms.Label
    Friend WithEvents textBox4 As System.Windows.Forms.TextBox
    Friend WithEvents label1 As System.Windows.Forms.Label
    Friend WithEvents textBox1 As System.Windows.Forms.TextBox
    Friend WithEvents label2 As System.Windows.Forms.Label
    Friend WithEvents textBox2 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.textBox3 = New System.Windows.Forms.TextBox
        Me.label3 = New System.Windows.Forms.Label
        Me.textBox4 = New System.Windows.Forms.TextBox
        Me.label1 = New System.Windows.Forms.Label
        Me.textBox1 = New System.Windows.Forms.TextBox
        Me.label2 = New System.Windows.Forms.Label
        Me.textBox2 = New System.Windows.Forms.TextBox
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.SystemColors.Window
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Panel1.Controls.Add(Me.textBox3)
        Me.Panel1.Controls.Add(Me.label3)
        Me.Panel1.Controls.Add(Me.textBox4)
        Me.Panel1.Controls.Add(Me.label1)
        Me.Panel1.Controls.Add(Me.textBox1)
        Me.Panel1.Controls.Add(Me.label2)
        Me.Panel1.Controls.Add(Me.textBox2)
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Panel1.Location = New System.Drawing.Point(0, 0)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(118, 18)
        Me.Panel1.TabIndex = 0
        '
        'textBox3
        '
        Me.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.textBox3.Location = New System.Drawing.Point(59, -1)
        Me.textBox3.Name = "textBox3"
        Me.textBox3.Size = New System.Drawing.Size(24, 14)
        Me.textBox3.TabIndex = 11
        Me.textBox3.Text = ""
        Me.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'label3
        '
        Me.label3.BackColor = System.Drawing.Color.Transparent
        Me.label3.Font = New System.Drawing.Font("Arial", 10.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.label3.Location = New System.Drawing.Point(83, 1)
        Me.label3.Name = "label3"
        Me.label3.Size = New System.Drawing.Size(4, 14)
        Me.label3.TabIndex = 12
        Me.label3.Text = "."
        Me.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'textBox4
        '
        Me.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.textBox4.Location = New System.Drawing.Point(87, -1)
        Me.textBox4.Name = "textBox4"
        Me.textBox4.Size = New System.Drawing.Size(24, 14)
        Me.textBox4.TabIndex = 13
        Me.textBox4.Text = ""
        Me.textBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'label1
        '
        Me.label1.BackColor = System.Drawing.Color.Transparent
        Me.label1.Font = New System.Drawing.Font("Arial", 10.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.label1.Location = New System.Drawing.Point(27, 3)
        Me.label1.Name = "label1"
        Me.label1.Size = New System.Drawing.Size(4, 10)
        Me.label1.TabIndex = 8
        Me.label1.Text = "."
        Me.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'textBox1
        '
        Me.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.textBox1.Location = New System.Drawing.Point(3, -1)
        Me.textBox1.Name = "textBox1"
        Me.textBox1.Size = New System.Drawing.Size(24, 14)
        Me.textBox1.TabIndex = 7
        Me.textBox1.Text = ""
        Me.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'label2
        '
        Me.label2.BackColor = System.Drawing.Color.Transparent
        Me.label2.Font = New System.Drawing.Font("Arial", 10.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.label2.Location = New System.Drawing.Point(55, 1)
        Me.label2.Name = "label2"
        Me.label2.Size = New System.Drawing.Size(4, 14)
        Me.label2.TabIndex = 10
        Me.label2.Text = "."
        Me.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'textBox2
        '
        Me.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.textBox2.Location = New System.Drawing.Point(31, -1)
        Me.textBox2.Name = "textBox2"
        Me.textBox2.Size = New System.Drawing.Size(24, 14)
        Me.textBox2.TabIndex = 9
        Me.textBox2.Text = ""
        Me.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'IpBox
        '
        Me.Controls.Add(Me.Panel1)
        Me.Name = "IpBox"
        Me.Size = New System.Drawing.Size(118, 18)
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)
    End Sub
#End Region

    Public strIP As String
    Public Property IPAddress() As String
        Get
            Dim IPBox() As TextBox = {textBox1, textBox2, textBox3, textBox4}
            strIP = ""
            For i As Integer = 0 To 3
                strIP += IIf(IPBox(i).Text.Trim() <> "", IPBox(i).Text.Trim(), "0")
                If i < 3 Then strIP += "."
            Next
            Return strIP
        End Get
        Set(ByVal Value As String)
            strIP = Value
            Dim arrIP As String() = strIP.Split(".")
            Dim IPBox() As TextBox = {textBox1, textBox2, textBox3, textBox4}
            If arrIP.Length = 4 Then
                For i As Integer = 0 To 3
                    Value = Convert.ToInt32(arrIP(i).Trim)
                    If Value < 0 Then
                        IPBox(i).Text = "0"
                    ElseIf Value > 255 Then
                        IPBox(i).Text = "255"
                    Else
                        IPBox(i).Text = Value.ToString
                    End If
                Next
            End If
        End Set
    End Property

    Private Sub IPText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textBox1.TextChanged, textBox2.TextChanged, textBox3.TextChanged, textBox4.TextChanged
        Dim IPBox() As TextBox = {textBox1, textBox2, textBox3, textBox4}
        If sender.Text.Trim <> "" Then
            Dim val As Integer = Convert.ToInt32(sender.Text.Trim)
            If val < 0 Then
                sender.Text = "0"
            ElseIf val > 255 Then
                sender.Text = "255"
                sender.SelectionStart = sender.TextLength
            End If
        End If
        For i As Integer = 0 To 3
            If sender.Equals(IPBox(i)) AndAlso i < 3 AndAlso IPBox(i).TextLength = 3 Then
                IPBox(i + 1).Focus()
                IPBox(i + 1).SelectAll()
            End If
        Next
    End Sub

    Private Sub IPText_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textBox1.KeyDown, textBox2.KeyDown, textBox3.KeyDown, textBox4.KeyDown
        Dim IPBox() As TextBox = {textBox1, textBox2, textBox3, textBox4}
        For i As Integer = 0 To 3
            If sender.Equals(IPBox(i)) Then
                Select Case e.KeyCode
                    Case Keys.Back
                        If i > 0 And IPBox(i).Text.Trim = "" Then
                            IPBox(i - 1).Focus()
                            IPBox(i - 1).SelectionStart = IPBox(i - 1).Text.Length
                        End If
                    Case Keys.Right
                        If i < 3 And IPBox(i).SelectionStart = IPBox(i).TextLength Then
                            IPBox(i + 1).Focus()
                            IPBox(i + 1).SelectAll()
                        End If
                    Case Keys.Left
                        If i > 0 And IPBox(i).SelectionStart = 0 Then
                            IPBox(i - 1).Focus()
                            IPBox(i - 1).SelectAll()
                        End If
                    Case Else
                        Exit Sub
                End Select
                Exit For
            End If
        Next
    End Sub

    Private Sub IPText_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textBox1.KeyPress, textBox2.KeyPress, textBox3.KeyPress, textBox4.KeyPress
        Dim IPBox() As TextBox = {textBox1, textBox2, textBox3, textBox4}
        If Char.IsControl(e.KeyChar) Then
        Else
            If Not Char.IsDigit(e.KeyChar) Then
                e.Handled = True
            End If
        End If
        If e.KeyChar = "." Then
            For i As Integer = 0 To 3
                If sender.Equals(IPBox(i)) And i < 3 Then
                    IPBox(i + 1).Focus()
                    IPBox(i + 1).SelectAll()
                End If
            Next
        End If
    End Sub

    Private Sub IpText_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
        Dim nTextBoxWidth As Integer = (Me.Width - 3 * label1.Width) / 4
        textBox1.Location = New Point(0, 2)
        textBox1.Width = nTextBoxWidth
        textBox1.Height = Me.Height
        label1.Left = textBox1.Right
        label1.Top = 0
        label1.Width = label1.Width
        label1.Height = Me.Height - 4
        textBox2.Left = label1.Right
        textBox2.Top = 2
        textBox2.Width = nTextBoxWidth
        textBox2.Height = Me.Height
        label2.Left = textBox2.Right
        label2.Top = 0
        label2.Width = label1.Width
        label2.Height = Me.Height - 4
        textBox3.Left = label2.Right
        textBox3.Top = 2
        textBox3.Width = nTextBoxWidth
        textBox3.Height = Me.Height
        label3.Left = textBox3.Right
        label3.Top = 0
        label3.Width = label1.Width
        label3.Height = Me.Height - 4
        textBox4.Left = label3.Right
        textBox4.Top = 2
        textBox4.Width = nTextBoxWidth
        textBox4.Height = Me.Height - 4
    End Sub
End Class

运行一下,控件面板上多了一个名为IpBox的用户自定义控件,将其拖到Form中,即可使用,它最重要的属性就是IpAddress,类型是字符串,取值时,就是输入框的输入结果,赋值时,新值会显示在输入框中。
这里写图片描述

  • 1
    点赞
  • 0
    评论
  • 2
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

xjnzhidao

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值