C# IPv4 控件

調用時 直接使用  private IPv4TextBox XXX;
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace BASARA
{
    class IPv4TextBox : 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>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }

            base.Dispose(disposing);
        }

        public IPv4TextBox()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </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.BackColor = System.Drawing.SystemColors.Window;
            this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.MaxLength = 3;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 12);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress);
            this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.IPv4TextBox_KeyUp);
            // 
            // textBox2
            // 
            this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox2.Location = new System.Drawing.Point(0, 0);
            this.textBox2.MaxLength = 3;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 12);
            this.textBox2.TabIndex = 1;
            this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress);
            this.textBox2.KeyUp += new System.Windows.Forms.KeyEventHandler(this.IPv4TextBox_KeyUp);
            // 
            // textBox3
            // 
            this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox3.Location = new System.Drawing.Point(0, 0);
            this.textBox3.MaxLength = 3;
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(100, 12);
            this.textBox3.TabIndex = 2;
            this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress);
            this.textBox3.KeyUp += new System.Windows.Forms.KeyEventHandler(this.IPv4TextBox_KeyUp);
            // 
            // textBox4
            // 
            this.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox4.Location = new System.Drawing.Point(0, 0);
            this.textBox4.MaxLength = 3;
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(100, 12);
            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.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(7, 12);
            this.label1.TabIndex = 4;
            this.label1.Text = ".";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(0, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(7, 12);
            this.label2.TabIndex = 5;
            this.label2.Text = ".";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(0, 0);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(7, 12);
            this.label3.TabIndex = 6;
            this.label3.Text = ".";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // IPv4TextBox
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Name = "IPv4TextBox";
            this.Size = new System.Drawing.Size(146, 21);
            this.ResumeLayout(false);
            this.PerformLayout();
        }

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

            if (keyChar == '.' || keyChar == ' ')
            {
                if ((((TextBox)sender).SelectedText.Length == 0) && (textLength > 0) && (((TextBox)sender) != textBox4))
                {
                    SendKeys.Send("{Tab}");
                    ((TextBox)(this.GetNextControl(this.ActiveControl, true))).SelectAll();
                }

                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)
                        {
                            SendKeys.Send("+{TAB}{End}");
                        }
                    }
                }
                else
                {
                    e.Handled = true;
                }

            }
        }

        private void IPv4TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            int textLength = ((TextBox)sender).TextLength;
            if (textLength == 3 && ((TextBox)sender).SelectedText.Length == 0)
            {
                SendKeys.Send("{Tab}");
                ((TextBox)(this.GetNextControl(this.ActiveControl, true))).SelectAll();

                e.Handled = true;

            }
            base.OnKeyUp(e);
        }

        public override 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");
                    }
                }
	        }
        }

        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");
                }
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            System.Drawing.Size textBoxSize = new System.Drawing.Size((this.Size.Width - 33) / 4, 19);

            this.textBox1.Size = textBoxSize;
            this.textBox2.Size = textBoxSize;
            this.textBox3.Size = textBoxSize;
            this.textBox4.Size = textBoxSize;

            this.textBox1.Location = new System.Drawing.Point(0, 5);
            this.label1.Location = this.textBox1.Location + new System.Drawing.Size(textBoxSize.Width + 2, 0);
            this.textBox2.Location = this.label1.Location + new System.Drawing.Size(9, 0);
            this.label2.Location = this.textBox2.Location + new System.Drawing.Size(textBoxSize.Width + 2, 0);
            this.textBox3.Location = this.label2.Location + new System.Drawing.Size(9, 0);
            this.label3.Location = this.textBox3.Location + new System.Drawing.Size(textBoxSize.Width + 2, 0);
            this.textBox4.Location = this.label3.Location + new System.Drawing.Size(9, 0);
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章