C#创建用户控件 - IPv4地址输入框

根据网上的改写:http://blog.csdn.net/jhqin/article/details/5823363

控件属性:

Text:获取或设置string类型的IP地址

Value:获取或设置IPAddress类型的IP地址

Type:获取IP地址所属分类。分A、B、C、D、E 5大类。

BorderStyle:获取或设置控件的边框样式。

ValidateIP() 判断ip是否有效
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;

namespace FVD.Common
{
    public enum IPType : byte { A, B, C, D, E };

    public class IPAddressTextBox : UserControl
    {
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.TextBox textBox4;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;

        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(0, 2);
            this.textBox1.MaxLength = 3;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(25, 21);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress);
            //
            // textBox2
            //
            this.textBox2.Location = new System.Drawing.Point(33, 2);
            this.textBox2.MaxLength = 3;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(25, 21);
            this.textBox2.TabIndex = 1;
            this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress);
            //
            // textBox3
            //
            this.textBox3.Location = new System.Drawing.Point(67, 2);
            this.textBox3.MaxLength = 3;
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(25, 21);
            this.textBox3.TabIndex = 2;
            this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress);
            //
            // textBox4
            //
            this.textBox4.Location = new System.Drawing.Point(100, 2);
            this.textBox4.MaxLength = 3;
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(25, 21);
            this.textBox4.TabIndex = 3;
            this.textBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.Location = new System.Drawing.Point(25, 5);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(16, 16);
            this.label1.TabIndex = 4;
            this.label1.Text = ".";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label2.Location = new System.Drawing.Point(57, 5);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(16, 16);
            this.label2.TabIndex = 5;
            this.label2.Text = ".";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label3.Location = new System.Drawing.Point(92, 5);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(16, 16);
            this.label3.TabIndex = 6;
            this.label3.Text = ".";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // IPAddressTextBox
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Name = "IPAddressTextBox";
            this.Size = new System.Drawing.Size(125, 28);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        public IPAddressTextBox()
        {
            InitializeComponent();
        }

        private void IPv4TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            char KeyChar = e.KeyChar;
            int TextLength = ((TextBox)sender).TextLength;

            if (KeyChar == ‘.‘ || KeyChar == ‘。‘ || KeyChar == ‘ ‘)
            {
                if ((((TextBox)sender).SelectedText.Length == 0) && (TextLength > 0) && (((TextBox)sender) != textBox4))
                {   // 进入下一个文本框
                    SendKeys.Send("{Tab}");
                }

                e.Handled = true;
            }

            if (Regex.Match(KeyChar.ToString(), "[0-9]").Success)
            {
                if (TextLength == 2)
                {
                    if (int.Parse(((TextBox)sender).Text + e.KeyChar.ToString()) > 255)
                    {
                        e.Handled = true;
                    }
                }
                else if (TextLength == 0)
                {
                    if (KeyChar == ‘0‘)
                    {
                        e.Handled = true;
                    }
                }
            }
            else
            {   // 回删操作
                if (KeyChar == ‘\b‘)
                {
                    if (TextLength == 0)
                    {
                        if (((TextBox)sender) != textBox1)
                        {   // 回退到上一个文本框 Shift+Tab
                            SendKeys.Send("+{TAB}{End}");
                        }
                    }
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

        /// <summary>
        /// string类型的IP地址
        /// </summary>
        override public string Text
        {
            get
            {
                return this.Value.ToString();
            }
            set
            {
                IPAddress address;
                if (IPAddress.TryParse(value, out address))
                {
                    byte[] bytes = address.GetAddressBytes();
                    for (int i = 1; i <= 4; i++)
                    {
                        this.Controls["textBox" + i.ToString()].Text = bytes[i - 1].ToString("D");
                    }
                }
            }
        }

        /// <summary>
        /// IP地址
        /// </summary>
        public IPAddress Value
        {
            get
            {
                IPAddress address;
                string ipString = textBox1.Text + "." + textBox2.Text + "." + textBox3.Text + "." + textBox4.Text;

                if (IPAddress.TryParse(ipString, out address))
                {
                    return address;
                }
                else
                {
                    return new IPAddress(0);
                }
            }
            set
            {
                byte[] bytes = value.GetAddressBytes();
                for (int i = 1; i <= 4; i++)
                {
                    this.Controls["textBox" + i.ToString()].Text = bytes[i - 1].ToString("D");
                }
            }
        }

        /// <summary>
        /// IP地址分类
        /// </summary>
        public IPType Type
        {
            get
            {
                byte[] bytes = this.Value.GetAddressBytes();
                int FirstByte = bytes[0];

                if (FirstByte < 128)
                {
                    return IPType.A;
                }
                else if (FirstByte < 192)
                {
                    return IPType.B;
                }
                else if (FirstByte < 224)
                {
                    return IPType.C;
                }
                else if (FirstByte < 240)
                {
                    return IPType.D;
                }
                else
                {
                    return IPType.E;    // 保留做研究用
                }
            }
        }

        public bool ValidateIP()
        {
            IPAddress address;
            string ipString = textBox1.Text + "." + textBox2.Text + "." + textBox3.Text + "." + textBox4.Text;

            return IPAddress.TryParse(ipString, out address);
        }

        /// <summary>
        /// 控件的边框样式
        /// </summary>
        new public BorderStyle BorderStyle
        {
            get
            {
                return this.textBox1.BorderStyle;
            }
            set
            {
                for (int i = 1; i <= 4; i++)
                {
                    ((TextBox)this.Controls["textBox" + i.ToString()]).BorderStyle = value;
                }
            }
        }
    }
}
时间: 2024-10-09 07:11:21

C#创建用户控件 - IPv4地址输入框的相关文章

wpf创建用户控件(计时器控件)

在vs中新增用户控件 前台xaml如下代码: <UserControl x:Class="Zh.SelfServiceEquipment.UI.ZhControls.CountDownTimeControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&qu

创建一个带模版的用户控件

今天学习与练习,做一个用户控件,让其带模版的.关键是两个接口ITemplate和INamingContainer,有关此两个接口技术诠释可以查MSDN官网. 创建一个控件,它继承Control和INamingContainer接口. 创建用户控件UcB.ascx,拉一个控件容器Placeholder控件,方便在cs能动态加载上面创建的控件 UcB.ascx.cs: 在网页xxx.aspx: xxx.aspx.cs: 运行结果:

asp.net 用户控件

转载自http://www.cnblogs.com/SkySoot/archive/2012/09/04/2670678.html 在 .NET 里,可以通过两种方式把自己的控件插入到 Web 窗体框架中: 用户控件:它是一小段页面,可以包括静态 HTML 代码和 Web 服务器控件.用户控件的好处是一旦创建了它,就可以在同一个 Web 应用程序的多个页面重用它.用户控件可以加入自己的属性,事件和方法. 自定义服务器控件:它是被编译的类,它通过编程生成自己的 HTML .服务器控件总是预编译到

(九)ASP.NET自定义用户控件(2)

http://www.cnblogs.com/SkySoot/archive/2012/09/04/2670678.html 用户控件 在 .NET 里,可以通过两种方式把自己的控件插入到 Web 窗体框架中: 用户控件:它是一小段页面,可以包括静态 HTML 代码和 Web 服务器控件.用户控件的好处是一旦创建了它,就可以在同一个 Web 应用程序的多个页面重用它.用户控件可以加入自己的属性,事件和方法. 自定义服务器控件:它是被编译的类,它通过编程生成自己的 HTML .服务器控件总是预编译

用户控件(UserControl)

简介 "用户控件"继承自UserControl,而UserControl继承自ContentControl,也就是内容控件UserControl和Window是一个层次上的,都有xaml和cs文件 流程 创建用户控件 写好用户控件 <UserControl x:Class="WpfDemo.UserControlDemo.OwnUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xa

1 asp.net 中如何把用户控件应用于母版页

1 创建用户控件 2 在母版页中注册用户控件 3 使用 <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Store.master.cs" Inherits="SportsStore.Pages.Store" %> <%@ Register TagPrefix="SS" TagName="CatLinks&qu

组件用户控件引用样式资源(二)---代码引用

关于样式文件引用方式存在以下弊端(摘自官方) When you reference a ResourceDictionary in XAML, a ResourceDictionary object is created each time you reference it.  So if you have 10 custom controls in your library and merge the shared ResourceDictionaries for each control b

创建WPF用户控件

wpf用户自定义控件和winform创建方法类似,这里先纠正一个误区,就是有很多人也是添加,然后新建,然后是新建用户控件库,但是为什么编译好生成后Debug目录下还是只有exe文件而没有dll文件呢?这里大家要注意一下,新建要在解决方案处右键新建-新建项目,而不是在项目下新建,因为一个项目只产生一个exe的程序文件,要生成dll文件必须新建一个项目.如图: 下面我用一个Tooltip举例新建一个用户控件: 先新建一个wpf程序,我这里命名为:"Wpf自定义控件".然后再新建一个项目,即

用户控件,动态创建添加用户控件

用户控件的制作步骤 1,新建一个项目,文件>新建>项目. 2,添加>新建项>用户控件.文件类型为.cs 使用用户控件 在用户控件设计业点击工具栏中的生成项,选择第一项生成解决方案,或直接点F6: 生成成功后在windows窗体的工具栏最顶端就会显示此用户控件 动态添加控件 输入一个值为n,点击按钮,动态生成n个按钮,并编号号 private void button1_Click(object sender, EventArgs e) { //先获取用户输入的是几 int count