65.9K
CodeProject 正在变化。 阅读更多。
Home

C# 中的 IpBox

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.11/5 (7投票s)

2005年5月9日

viewsIcon

55954

downloadIcon

306

一种创建 IP 输入框的简单方法。

引言

这只是一个简单的 IpBox 控件,就像 MFC 中的一样。

使用代码

这个简单的 IpBox 实际上由一个 Panel 作为背景、四个 TextBox 控件放置在适当的位置以收集用户输入,以及三个 Label 控件表示句点组成,就像所有其他的 IP 地址控件一样。将四个 TextBox 控件和三个 Label 控件添加到面板中并按顺序排列。然后,基本上就完成了界面。剩下的只是验证地址输入逻辑。

让我们来看一下代码

// 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;
    }
}

就这样。您可以参考源代码获取完整代码。要使用它,只需遵循以下三个步骤:

  1. 将您的 DLL 添加为引用。
  2. 在您的 using 部分添加一行:"using IpBox;"
  3. 声明
    private IpBox.IpBox ipbox = new IpBox.IpBox();
    ipbox.GetIPAddress();// it returns System.Net.IPAddress

三个步骤就完成了。

更新

由于这是我的第一篇文章,而且我仍在学习,请提供有关需要修改的部分的评论以及对不良编程技术的更正。

附言:我将一个位图添加到资源中,以便您可以将其添加到工具箱中,并使用一个简单的 ipbox 图片 :) 享受吧!

© . All rights reserved.