Components

A Specialised TextBox for IPv4 Address Entry

This is a C# component, so first of all create an UserControl class called IPAddress. This most commonly done within a ComponentLibrary project. Pull a GroupBox from the toolbox on to the component design form. We now have the IPAddress.cs class, a Designer.cs and a resource IPAddress.resx file. Now just substitute the code with that listed here.

The IP Address Collection

The IPAddress component is a group box called IPGroup, inside of which we place four NumericalTextBox and three DotLabel components

    public partial class IPAddress : UserControl
    {
        public IPAddress()
        {
            InitializeComponent();
            Byte[] ip_address = new byte[] { 0, 0, 0, 0 };
            IP = new System.Net.IPAddress(ip_address);
        }

        public Color BackgroundColour
        {
            get
            {
                try { return IPGroup.BackColor; }
                catch { return Color.AntiqueWhite; }
            }
            set
            {
                IPGroup.BackColor = value;
                Dot_1.BackColor = value;
                Dot_2.BackColor = value;
                Dot_3.BackColor = value;
                mIPBox_1.BackColor = value;
                mIPBox_2.BackColor = value;
                mIPBox_3.BackColor = value;
                mIPBox_4.BackColor = value;
            }
        }

        public Color Text_BackgroundColour
        {
            get
            {
                try { return mIPBox_1.Text_Background; }
                catch { return Color.AntiqueWhite; }
            }
            set
            {
                mIPBox_1.Text_Background = value;
                mIPBox_2.Text_Background = value;
                mIPBox_3.Text_Background = value;
                mIPBox_4.Text_Background = value;
            }
        }

        public System.Net.IPAddress IP
        {
            get
            {
                try
                {
                    if (mIPBox_1.Text.Length < 1) mIPBox_1.IntValue = 0;
                    if (mIPBox_2.Text.Length < 1) mIPBox_2.IntValue = 0;
                    if (mIPBox_3.Text.Length < 1) mIPBox_3.IntValue = 0;
                    if (mIPBox_4.Text.Length < 1) mIPBox_4.IntValue = 0;
                    Byte[] IP_bytes = new Byte[4] { Convert.ToByte(mIPBox_1), Convert.ToByte(mIPBox_2), Convert.ToByte(mIPBox_3), Convert.ToByte(mIPBox_4) };
                    return new System.Net.IPAddress(IP_bytes);
                }
                catch
                {
                    Byte[] ip_bytes = new Byte[] { 0, 0, 0, 0 };
                    return new System.Net.IPAddress(ip_bytes);
                }
            }
            set
            {
                try
                {
                    Byte[] IP_bytes = value.GetAddressBytes();
                    mIPBox_1.IntValue = IP_bytes[0];
                    mIPBox_2.IntValue = IP_bytes[1];
                    mIPBox_3.IntValue = IP_bytes[2];
                    mIPBox_4.IntValue = IP_bytes[3];
                }
                catch
                {
                    mIPBox_1.IntValue = 0;
                    mIPBox_2.IntValue = 0;
                    mIPBox_3.IntValue = 0;
                    mIPBox_4.IntValue = 0;
                }
            }
        }
    }

The base 10 type is used with the range limited to 0 to 255. The alternative of hex input requires changing the NumericalTextBox properties. An IPAddress class is used for I/O of the parameters. Padding with 0 is used to ensure the text displayed is always 3 digits.

        private void InitializeComponent()
        {
            this.IPGroup = new System.Windows.Forms.GroupBox();
            this.mIPBox_3 = new ComponentLibrary.NumericalTextBox();
            this.Dot_3 = new ComponentLibrary.DotLabel();
            this.Dot_2 = new ComponentLibrary.DotLabel();
            this.Dot_1 = new ComponentLibrary.DotLabel();
            this.mIPBox_2 = new ComponentLibrary.NumericalTextBox();
            this.mIPBox_1 = new ComponentLibrary.NumericalTextBox();
            this.mIPBox_4 = new ComponentLibrary.NumericalTextBox();
            this.IPGroup.SuspendLayout();
            this.SuspendLayout();
            // 
            // IPGroup
            // 
            this.IPGroup.BackColor = System.Drawing.Color.LightSkyBlue;
            this.IPGroup.Controls.Add(this.mIPBox_3);
            this.IPGroup.Controls.Add(this.Dot_3);
            this.IPGroup.Controls.Add(this.Dot_2);
            this.IPGroup.Controls.Add(this.Dot_1);
            this.IPGroup.Controls.Add(this.mIPBox_2);
            this.IPGroup.Controls.Add(this.mIPBox_1);
            this.IPGroup.Controls.Add(this.mIPBox_4);
            this.IPGroup.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.IPGroup.Location = new System.Drawing.Point(4, 4);
                    this.IPGroup.MaximumSize = new System.Drawing.Size(180, 35);
                    this.IPGroup.MinimumSize = new System.Drawing.Size(180, 35);
                    this.IPGroup.Name = "IPGroup";
                    this.IPGroup.Size = new System.Drawing.Size(180, 35);
                    this.IPGroup.TabIndex = 0;
                    this.IPGroup.TabStop = false;
                    this.IPGroup.Text = "IP";
                    // 
                    // mIPBox_3
                    // 
                    this.mIPBox_3.AlignText = System.Windows.Forms.HorizontalAlignment.Center;
                    this.mIPBox_3.AllowBinary = false;
                    this.mIPBox_3.AllowDecimal = true;
                    this.mIPBox_3.AllowHex = false;
                    this.mIPBox_3.AllowNegativeSign = false;
                    this.mIPBox_3.AllowNibble = false;
                    this.mIPBox_3.AllowOctel = false;
                    this.mIPBox_3.AllowPlus = false;
                    this.mIPBox_3.AllowSpace = false;
                    this.mIPBox_3.BackColor = System.Drawing.SystemColors.Control;
                    this.mIPBox_3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.mIPBox_3.IntValue = 0;
                    this.mIPBox_3.Location = new System.Drawing.Point(104, 12);
                    this.mIPBox_3.Margin = new System.Windows.Forms.Padding(0);
                    this.mIPBox_3.MaxDigit = 3;
                    this.mIPBox_3.MaximumIntegerValue = 255;
                    this.mIPBox_3.MinimumIntegerValue = 0;
                    this.mIPBox_3.Name = "mIPBox_3";
                    this.mIPBox_3.Size = new System.Drawing.Size(30, 20);
                    this.mIPBox_3.SizeTextBox = new System.Drawing.Size(30, 20);
                    this.mIPBox_3.TabIndex = 7;
                    this.mIPBox_3.Text_Background = System.Drawing.Color.LightSteelBlue;
                    this.mIPBox_3.TextPad = true;
                    this.mIPBox_3.TextPadTo = 3;
                    // 
                    // Dot_3
                    // 
                    this.Dot_3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.Dot_3.Location = new System.Drawing.Point(134, 12);
                    this.Dot_3.Margin = new System.Windows.Forms.Padding(0);
                    this.Dot_3.Name = "Dot_3";
                    this.Dot_3.Size = new System.Drawing.Size(12, 20);
                    this.Dot_3.TabIndex = 6;
                    this.Dot_3.TabStop = false;
                    // 
                    // Dot_2
                    // 
                    this.Dot_2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.Dot_2.Location = new System.Drawing.Point(92, 12);
                    this.Dot_2.Margin = new System.Windows.Forms.Padding(0);
                    this.Dot_2.Name = "Dot_2";
                    this.Dot_2.Size = new System.Drawing.Size(12, 20);
                    this.Dot_2.TabIndex = 5;
                    this.Dot_2.TabStop = false;
                    // 
                    // Dot_1
                    // 
                    this.Dot_1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.Dot_1.Location = new System.Drawing.Point(50, 12);
                    this.Dot_1.Margin = new System.Windows.Forms.Padding(0);
                    this.Dot_1.Name = "Dot_1";
                    this.Dot_1.Size = new System.Drawing.Size(12, 20);
                    this.Dot_1.TabIndex = 4;
                    this.Dot_1.TabStop = false;
                    // 
                    // mIPBox_2
                    // 
                    this.mIPBox_2.AlignText = System.Windows.Forms.HorizontalAlignment.Center;
                    this.mIPBox_2.AllowBinary = false;
                    this.mIPBox_2.AllowDecimal = true;
                    this.mIPBox_2.AllowHex = false;
                    this.mIPBox_2.AllowNegativeSign = false;
                    this.mIPBox_2.AllowNibble = false;
                    this.mIPBox_2.AllowOctel = false;
                    this.mIPBox_2.AllowPlus = false;
                    this.mIPBox_2.AllowSpace = false;
                    this.mIPBox_2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.mIPBox_2.IntValue = 0;
                    this.mIPBox_2.Location = new System.Drawing.Point(62, 12);
                    this.mIPBox_2.Margin = new System.Windows.Forms.Padding(0);
                    this.mIPBox_2.MaxDigit = 3;
                    this.mIPBox_2.MaximumIntegerValue = 255;
                    this.mIPBox_2.MinimumIntegerValue = 0;
                    this.mIPBox_2.Name = "mIPBox_2";
                    this.mIPBox_2.Size = new System.Drawing.Size(30, 20);
                    this.mIPBox_2.SizeTextBox = new System.Drawing.Size(30, 20);
                    this.mIPBox_2.TabIndex = 2;
                    this.mIPBox_2.Text_Background = System.Drawing.Color.LightSteelBlue;
                    this.mIPBox_2.TextPad = true;
                    this.mIPBox_2.TextPadTo = 3;
                    // 
                    // mIPBox_1
                    // 
                    this.mIPBox_1.AlignText = System.Windows.Forms.HorizontalAlignment.Center;
                    this.mIPBox_1.AllowBinary = false;
                    this.mIPBox_1.AllowDecimal = true;
                    this.mIPBox_1.AllowHex = false;
                    this.mIPBox_1.AllowNegativeSign = false;
                    this.mIPBox_1.AllowNibble = false;
                    this.mIPBox_1.AllowOctel = false;
                    this.mIPBox_1.AllowPlus = false;
                    this.mIPBox_1.AllowSpace = false;
                    this.mIPBox_1.BackColor = System.Drawing.SystemColors.Control;
                    this.mIPBox_1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.mIPBox_1.IntValue = 2;
                    this.mIPBox_1.Location = new System.Drawing.Point(20, 12);
                    this.mIPBox_1.Margin = new System.Windows.Forms.Padding(0);
                    this.mIPBox_1.MaxDigit = 3;
                    this.mIPBox_1.MaximumIntegerValue = 255;
                    this.mIPBox_1.MaximumSize = new System.Drawing.Size(30, 20);
                    this.mIPBox_1.MinimumIntegerValue = 0;
                    this.mIPBox_1.MinimumSize = new System.Drawing.Size(30, 20);
                    this.mIPBox_1.Name = "mIPBox_1";
                    this.mIPBox_1.Size = new System.Drawing.Size(30, 20);
                    this.mIPBox_1.SizeTextBox = new System.Drawing.Size(30, 20);
                    this.mIPBox_1.TabIndex = 1;
                    this.mIPBox_1.Text_Background = System.Drawing.Color.LightSteelBlue;
                    this.mIPBox_1.TextPad = true;
                    this.mIPBox_1.TextPadTo = 3;
                    // 
                    // mIPBox_4
                    // 
                    this.mIPBox_4.AlignText = System.Windows.Forms.HorizontalAlignment.Center;
                    this.mIPBox_4.AllowBinary = false;
                    this.mIPBox_4.AllowDecimal = true;
                    this.mIPBox_4.AllowHex = false;
                    this.mIPBox_4.AllowNegativeSign = false;
                    this.mIPBox_4.AllowNibble = false;
                    this.mIPBox_4.AllowOctel = false;
                    this.mIPBox_4.AllowPlus = false;
                    this.mIPBox_4.AllowSpace = false;
                    this.mIPBox_4.BackColor = System.Drawing.SystemColors.Control;
                    this.mIPBox_4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    this.mIPBox_4.IntValue = 0;
                    this.mIPBox_4.Location = new System.Drawing.Point(146, 12);
                    this.mIPBox_4.Margin = new System.Windows.Forms.Padding(0);
                    this.mIPBox_4.MaxDigit = 3;
                    this.mIPBox_4.MaximumIntegerValue = 255;
                    this.mIPBox_4.MinimumIntegerValue = 0;
                    this.mIPBox_4.Name = "mIPBox_4";
                    this.mIPBox_4.Size = new System.Drawing.Size(30, 20);
                    this.mIPBox_4.SizeTextBox = new System.Drawing.Size(30, 20);
                    this.mIPBox_4.TabIndex = 0;
                    this.mIPBox_4.Text_Background = System.Drawing.Color.LightSteelBlue;
                    this.mIPBox_4.TextPad = true;
                    this.mIPBox_4.TextPadTo = 3;
                    // 
                    // IPAddress
                    // 
                    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                    this.Controls.Add(this.IPGroup);
                    this.Name = "IPAddress";
                    this.Size = new System.Drawing.Size(183, 40);
                    this.IPGroup.ResumeLayout(false);
                    this.ResumeLayout(false);

                }

        private System.Windows.Forms.GroupBox IPGroup;
        private NumericalTextBox mIPBox_1;
        private NumericalTextBox mIPBox_4;
        private NumericalTextBox mIPBox_2;
        private DotLabel Dot_1;
        private DotLabel Dot_2;
        private DotLabel Dot_3;
        private NumericalTextBox mIPBox_3;
    }

The designer can be copied or the equivalent generated by hand by changing the properties of the designer form. The properties must coincide with that required by the .cs code. Note: only InitializeComponent() and declarations are here, the rest of the designer does not change!

    public partial class DotLabel : UserControl
    {
        public DotLabel()
        {
            InitializeComponent();
            this.Dot.Text = " - ";
        }

        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
        public String Display { set { this.Dot.Text = value; } }
    }
    private void InitializeComponent()
    {
        this.Dot = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // Dot
        // 
        this.Dot.CausesValidation = false;
        this.Dot.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Dot.Location = new System.Drawing.Point(0, 0);
        this.Dot.MaximumSize = new System.Drawing.Size(12, 20);
        this.Dot.MinimumSize = new System.Drawing.Size(12, 20);
        this.Dot.Name = "Dot";
        this.Dot.Size = new System.Drawing.Size(12, 20);
        this.Dot.TabIndex = 0;
        this.Dot.Text = "-";
        this.Dot.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        // 
        // DotLabel
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.Dot);
        this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Margin = new System.Windows.Forms.Padding(0);
        this.Name = "DotLabel";
        this.Size = new System.Drawing.Size(73, 55);
        this.ResumeLayout(false);

    }
    private System.Windows.Forms.Label Dot;

The DotLabel component is so simple it is hardly worth a mention but... it is required!