65.9K
CodeProject is changing. Read more.
Home

IpBox in C# for beginners

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.11/5 (7 votes)

May 9, 2005

viewsIcon

55752

downloadIcon

306

A simple way to create an IP box.

Introduction

This is just a simple IpBox control just like the one in MFC.

Using the code

This simple IpBox is actually made out of one Panel as the background, four TextBoxes in proper place to collect input from users and three Labels which represent full-stop just like all the other IP address controls. Add the four TextBoxes and three Labels to the panel and place them in order. Then you're basically done with the interface. The rest is just logic to validate the address input.

Let's have a look at some of the code:

// The class which inherits this interface will have
// to declare all the abstract functions in the interface
// it is implemented outside the class and inside the same namespace
// with the class which suppose to inherit this interface.
public interface IpBox_Control
{
    IPAddress GetIPAddress { set; get; }
}
// NOTE: this is just a portion of declared variables.
private TextBox ip1;
private TextBox ip2;
private TextBox ip3;
private TextBox ip4;
private string address;

// This function is inherited from the interface. It has no other purpose
// getting the ip address into string form and then convert it to IPAddress form
public IPAddress GetIPAddress
{
    get
    {
        // if the respective textbox is empty, it returns 0.
        address = 
         int.Parse( ( this.ip1.Text.Length > 0 ) ? this.ip1.Text : "0" ).ToString()
         + "." + 
         int.Parse( ( this.ip2.Text.Length > 0 ) ? this.ip2.Text : "0" ).ToString() 
         + "." + 
         int.Parse( ( this.ip3.Text.Length > 0 ) ? this.ip3.Text : "0" ).ToString() 
         + "." + 
         int.Parse( ( this.ip4.Text.Length > 0 ) ? this.ip4.Text : "0" ).ToString();

        return IPAddress.Parse( address );
    }
    set{ }
}

// this function is to validate input from user.
// Everytime a user input a value, it will go through this function
// If the value is not integer value, it will erase the value in the textbox
// If the value inserted is bigger than 255, the value will be set to 255.
// Each textbox can only accomodate a maximum of 3 characters to restrict
// the maximum value.
private void OnTextChange(object sender, System.EventArgs e)
{
    int box_type = 0;

    CultureInfo MyCultureInfo = new CultureInfo("en-GB");

    double d;

    // performing simple check to determine which textbox is currently
    // on focus
    if( sender.Equals( ip1 ) )
        box_type = 1;
    if( sender.Equals( ip2 ) )
        box_type = 2;
    if( sender.Equals( ip3 ) )
        box_type = 3;
    if( sender.Equals( ip4 ) )
        box_type = 4;

    switch( box_type )
    {
        case 1:
        // this is to check whether the user has inserted '.'
        // so that the focus can shift to the next textbox
        if( this.ip1.Text.Length > 0 && 
            this.ip1.Text.ToCharArray()[this.ip1.Text.Length - 1] == '.' )
        {
                // before we shift focus, we must first
                // erase the '.' inserted
                this.ip1.Text = this.ip1.Text.TrimEnd( '.' );
                this.ip1.Text = this.Parse( this.ip1.Text ).ToString();
                // set focus to the next textbox
                ip2.Focus();
            }

            // validate input whether it is still an integer value or not
            if( double.TryParse(
                this.ip1.Text,
                System.Globalization.NumberStyles.Integer,
                MyCultureInfo,
                out d ) == false
                )
            {
                // erase the whole textbox and wait for input again
                // as the previous input was invalid
                this.ip1.Text = this.ip1.Text.Remove( 0, this.ip1.Text.Length );
                return;
            }

            // change focus to the next textbox if the textbox
            // is fully occupied.
            if( this.ip1.Text.Length == 3 )
            {
                if( int.Parse( this.ip1.Text ) >= 255 )
                    this.ip1.Text = "255";
                else
                    this.ip1.Text = int.Parse( this.ip1.Text ).ToString();
                ip2.Focus();
            }
            break;
        case 2:
            if( this.ip2.Text.Length > 0 && 
              this.ip2.Text.ToCharArray()[this.ip2.Text.Length - 1] == '.' )
            {
                this.ip2.Text = this.ip2.Text.TrimEnd( '.' );
                this.ip2.Text = this.Parse( this.ip2.Text ).ToString();
                ip3.Focus();
            }

            if( double.TryParse(
                this.ip2.Text,
                System.Globalization.NumberStyles.Integer,
                MyCultureInfo,
                out d ) == false
                )
            {
                this.ip2.Text = this.ip2.Text.Remove( 0, this.ip2.Text.Length );
                return;
            }

            if( this.ip2.Text.Length == 3 )
            {
                if( int.Parse( this.ip2.Text ) >= 255 )
                    this.ip2.Text = "255";
                else
                    this.ip2.Text = int.Parse( this.ip2.Text ).ToString();
                ip3.Focus();
            }
            break;
        case 3:
            if( this.ip3.Text.Length > 0 && 
              this.ip3.Text.ToCharArray()[this.ip3.Text.Length - 1] == '.' )
            {
                this.ip3.Text = this.ip3.Text.TrimEnd( '.' );
                this.ip3.Text = this.Parse( this.ip3.Text ).ToString();
                ip4.Focus();
            }

            if( double.TryParse(
                this.ip3.Text,
                System.Globalization.NumberStyles.Integer,
                MyCultureInfo,
                out d ) == false
                )
            {
                this.ip3.Text = this.ip3.Text.Remove( 0, this.ip3.Text.Length );
                return;
            }

            if( this.ip3.Text.Length == 3 )
            {
                if( int.Parse( this.ip3.Text ) >= 255 )
                    this.ip3.Text = "255";
                else
                    this.ip3.Text = int.Parse( this.ip3.Text ).ToString();
                ip4.Focus();
            }
            break;
        case 4:

            if( double.TryParse(
                this.ip4.Text,
                System.Globalization.NumberStyles.Integer,
                MyCultureInfo,
                out d ) == false
                )
            {
                this.ip4.Text = this.ip4.Text.Remove( 0, this.ip4.Text.Length );
                return;
            }

            if( this.ip4.Text.Length == 3 )
            {
                if( int.Parse( this.ip4.Text ) >= 255 )
                    this.ip4.Text = "255";
                else
                    this.ip4.Text = int.Parse( this.ip4.Text ).ToString();
            }
            break;
    }
}

That's all. You can refer the source code for the full code. To use it, simply follow these three steps:

  1. Add your DLL as your reference.
  2. Add a line in your using section: "using IpBox;"
  3. Declaration:
    private IpBox.IpBox ipbox = new IpBox.IpBox();
    ipbox.GetIPAddress();// it returns System.Net.IPAddress

Three steps and that's it.

Update

Since this is my very first article, and I'm still learning, please do give comments on the portion that needs to be revised and corrections to bad programming techniques.

P.S.: I've added a bitmap into the resource so that you can add it into the toolbox with a simple ipbox picture :) Enjoy!