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 TextBox
es in proper place to collect input from users and three Label
s which represent full-stop just like all the other IP address controls. Add the four TextBox
es and three Label
s 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:
public interface IpBox_Control
{
IPAddress GetIPAddress { set; get; }
}
private TextBox ip1;
private TextBox ip2;
private TextBox ip3;
private TextBox ip4;
private string address;
public IPAddress GetIPAddress
{
get
{
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{ }
}
private void OnTextChange(object sender, System.EventArgs e)
{
int box_type = 0;
CultureInfo MyCultureInfo = new CultureInfo("en-GB");
double d;
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:
if( this.ip1.Text.Length > 0 &&
this.ip1.Text.ToCharArray()[this.ip1.Text.Length - 1] == '.' )
{
this.ip1.Text = this.ip1.Text.TrimEnd( '.' );
this.ip1.Text = this.Parse( this.ip1.Text ).ToString();
ip2.Focus();
}
if( double.TryParse(
this.ip1.Text,
System.Globalization.NumberStyles.Integer,
MyCultureInfo,
out d ) == false
)
{
this.ip1.Text = this.ip1.Text.Remove( 0, this.ip1.Text.Length );
return;
}
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:
- Add your DLL as your reference.
- Add a line in your
using
section: "using IpBox;
"
- Declaration:
private IpBox.IpBox ipbox = new IpBox.IpBox();
ipbox.GetIPAddress();
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!
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