Click here to Skip to main content
15,920,633 members
Articles / Programming Languages / C#
Article

IpBox in C# for beginners

Rate me:
Please Sign up or sign in to vote.
2.11/5 (7 votes)
9 May 2005 55.5K   306   18   4
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:

C#
// 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; }
}
C#
// 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:
    C#
    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!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Malaysia Malaysia
Learning is the key to self development and success. I am no expert in anything but I am learning to become 1. Wish me luck!

Visual Studio 6.0
Visual Studio.NET
C, C++, C#.NET, MFC, MSSQL
PHP, MYSQL

Learning MSSQL, ASP.NET, ASP2.NET

Comments and Discussions

 
GeneralLicense for this code Pin
Zirconflexe25-May-11 21:00
Zirconflexe25-May-11 21:00 
GeneralSetting the IP of the control. Pin
GraemeWoodhouse28-Feb-07 0:19
GraemeWoodhouse28-Feb-07 0:19 
I didnt see a way to set the IP address so i added a simple getset for an IP String.

        public String IP<br />
        {<br />
            get<br />
            {<br />
                address = int.Parse((ip1.Text.Length > 0) ? ip1.Text : "0").ToString()<br />
                            + "." +<br />
                            int.Parse((ip2.Text.Length > 0) ? ip2.Text : "0").ToString()<br />
                            + "." +<br />
                            int.Parse((ip3.Text.Length > 0) ? ip3.Text : "0").ToString()<br />
                            + "." +<br />
                            int.Parse((ip4.Text.Length > 0) ? ip4.Text : "0").ToString();<br />
<br />
                return address;<br />
            }<br />
            set<br />
            {<br />
                ArrayList ip = new ArrayList();<br />
                for (int x = 0; x < 3; x++)<br />
                {<br />
                    int dotOccurance = value.IndexOf(".");<br />
                    ip.Add(value.Substring(0, dotOccurance));<br />
                    value = value.Substring(dotOccurance+1);<br />
                }<br />
                ip1.Text = ip[0].ToString();<br />
                ip2.Text = ip[1].ToString();<br />
                ip3.Text = ip[2].ToString();<br />
                ip4.Text = value;<br />
            }<br />
        }

GeneralUse custom controls Pin
malharone9-May-05 9:43
malharone9-May-05 9:43 
GeneralRe: Use custom controls Pin
Mervick9-May-05 15:40
Mervick9-May-05 15:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.