C#自定义IP输入框控件

 更新时间:2020年6月25日 11:16  点击:2687

场景: 做一些网络设备应用时,需要有ip地址的输入,这就需要ip地址输入框控件

思路:

       1 重写TextBox 为IpInputBox。

        2 重写TextBox为SubIpInputBox

        3 一个IpInputBox 添加4个SubIpInputBox 和3个Label

控件图:

具体代码:

主窗口文件   Dialog.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
 
namespace IP 
{ 
  public delegate void FallBackEvent(IpInputBox box, int flag); 
 
  public partial class Dialog : Form 
  { 
    public Dialog() 
    { 
      InitializeComponent(); 
    } 
 
    private void Form1_Load(object sender, EventArgs e) 
    { 
      this.Controls.Add(ipBox); 
      ipBox.Location = new Point(10,10); 
    } 
 
    private IpInputBox ipBox = new IpInputBox(false); 
  } 
} 

IpInputBox.cs 文件

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 
 
namespace IP 
{ 
  public class IpInputBox :TextBox 
  { 
    private bool _isNetmask = false; 
    public bool IsNetmask 
    { 
      get { return _isNetmask; } 
      set { _isNetmask = value; } 
    } 
    public IpInputBox(bool isNetmask) 
    { 
      _isNetmask = isNetmask; 
 
      this.Size = new System.Drawing.Size(150, 25); 
 
      _dotLabel1.Text = "."; 
      _dotLabel2.Text = "."; 
      _dotLabel3.Text = "."; 
 
      _dotLabel1.Size = new System.Drawing.Size(10, 25); 
      _dotLabel2.Size = new System.Drawing.Size(10, 25); 
      _dotLabel3.Size = new System.Drawing.Size(10, 25); 
 
      _box1.IsNetmask = _isNetmask; 
      _box2.IsNetmask = _isNetmask; 
      _box3.IsNetmask = _isNetmask; 
      _box4.IsNetmask = _isNetmask; 
 
      _box1.Flag = 1; 
      _box2.Flag = 2; 
      _box3.Flag = 3; 
      _box4.Flag = 4; 
 
 
      this.Controls.Add(_box1); 
      this.Controls.Add(_dotLabel1); 
 
 
      this.Controls.Add(_box2); 
      this.Controls.Add(_dotLabel2); 
 
 
      this.Controls.Add(_box3); 
      this.Controls.Add(_dotLabel3); 
 
      this.Controls.Add(_box4); 
 
      this.Font = new System.Drawing.Font(this.Font.Name, 11); 
      _box1.Location = new System.Drawing.Point(-1, 2); 
      _dotLabel1.Location = new System.Drawing.Point(29, 2); 
      _box2.Location = new System.Drawing.Point(39, 2); 
      _dotLabel2.Location = new System.Drawing.Point(69, 2); 
      _box3.Location = new System.Drawing.Point(79, 2); 
      _dotLabel3.Location = new System.Drawing.Point(109, 2); 
      _box4.Location = new System.Drawing.Point(119, 2); 
 
      _box1.Box = this; 
      _box2.Box = this; 
      _box3.Box = this; 
      _box4.Box = this; 
    } 
    public void FallBackEventFun(IpInputBox box, int flag) 
    { 
      switch (flag) 
      { 
        case 1: 
          _box1.Focus(); 
          break; 
        case 2: 
          _box1.Focus(); 
          break; 
        case 3: 
          _box2.Focus(); ; 
          break; 
        case 4: 
          _box3.Focus(); ; 
          break; 
      } 
    } 
 
    private string _ipAddress = string.Empty; 
    public void UpDateIpaddress() 
    { 
      try 
      { 
        string[] sArray = ipAddress.Split(new char[] { '.' }); 
        _box1.Text = sArray[0]; 
        _box2.Text = sArray[1]; 
        _box3.Text = sArray[2]; 
        _box4.Text = sArray[3]; 
      } 
      catch (Exception exp) 
      { 
        MessageBox.Show("更新字符串失败:" + exp.Message); 
      } 
    } 
 
    /// <summary> 
    /// 获取ip地址 
    /// </summary> 
    public string IpAddressString 
    { 
      get 
      { 
        string _ipStr1 = _box1.Text; 
        string _ipStr2 = _box2.Text; 
        string _ipStr3 = _box3.Text; 
        string _ipStr4 = _box4.Text; 
        string _ipDotStr = "."; 
        _ipAddress = _ipStr1 + _ipDotStr + _ipStr2 + _ipDotStr + _ipStr3 + _ipDotStr + _ipStr4; 
        return _ipAddress; 
      } 
      set 
      { 
        _ipAddress = value; 
      } 
    } 
    private string ipAddress = string.Empty; 
 
    public string IpAddress 
    { 
      get { return ipAddress; } 
      set { ipAddress = value; } 
    } 
 
    private SubIpInputBox _box1 = new SubIpInputBox(""); 
    private SubIpInputBox _box2 = new SubIpInputBox(""); 
    private SubIpInputBox _box3 = new SubIpInputBox(""); 
    private SubIpInputBox _box4 = new SubIpInputBox(""); 
 
    private Label _dotLabel1 = new Label(); 
    private Label _dotLabel2 = new Label(); 
    private Label _dotLabel3 = new Label(); 
 
  } 
} 

SubIpInputBox.cs 文件

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Drawing; 
using System.Windows.Forms; 
 
namespace IP 
{ 
  public class SubIpInputBox:TextBox 
  { 
      /// <summary> 
      /// 判断是否是子网掩码,false表示ip地址,true表示子网掩码 
      /// </summary> 
      private bool _isNetmask = false; 
      private bool _isSendKey = false; 
      private System.ComponentModel.IContainer components; 
      private int _flag = 0; 
 
      public int Flag 
      { 
        get { return _flag; } 
        set { _flag = value; } 
      } 
 
      /// <summary> 
      ///  
      /// </summary> 
      public event FallBackEvent TextFallBackEvent; 
 
 
      //定义事件函数 
      public void FallBackEventFun(int flag) 
      { 
        if (TextFallBackEvent != null) 
        { 
          TextFallBackEvent(box, this.Flag); 
        } 
      } 
 
      public bool IsNetmask 
      { 
        get { return _isNetmask; } 
        set { _isNetmask = value; } 
      } 
 
      /// <summary> 
      /// 构造函数 
      /// </summary> 
      public SubIpInputBox(bool isNetmask) 
      { 
        _isNetmask = isNetmask; 
        box = new IpInputBox(_isNetmask); 
        this.Font = new System.Drawing.Font(this.Font.Name, 11); 
        this.BorderStyle = System.Windows.Forms.BorderStyle.None;//去掉边框 
        this.TextAlign = HorizontalAlignment.Center;//字体居中 
        this.Size = new System.Drawing.Size(30, 25); 
        this.MaxLength = 3; 
      } 
 
      public SubIpInputBox(string str) 
      { 
        this.Text = str; 
        this.Size = new System.Drawing.Size(30, 25); 
        this.MaxLength = 3; 
        this.BorderStyle = System.Windows.Forms.BorderStyle.None;//去掉边框 
        this.TextAlign = HorizontalAlignment.Center;//字体居中 
      } 
 
      private IpInputBox box; 
      public IpInputBox Box 
      { 
        get { return box; } 
        set { box = value; } 
      } 
 
      protected override void OnKeyUp(KeyEventArgs e) 
      { 
        base.OnKeyUp(e); 
 
 
        if (this.Text == "") 
        { 
          if (e.KeyCode.ToString() == "Back") 
          { 
            this.TextFallBackEvent += new FallBackEvent(box.FallBackEventFun); 
            this.FallBackEventFun(this.Flag); 
          } 
        } 
      } 
      protected override void OnKeyPress(KeyPressEventArgs e) 
      { 
        base.OnKeyPress(e); 
 
        //阻止从键盘输入键 
        e.Handled = true; 
 
 
        if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == (char)8)) 
        { 
 
          if ((e.KeyChar == (char)8)) 
          { 
            e.Handled = false; return; 
          } 
          else 
          { 
            int len = this.Text.Length; 
            if (len < 4) 
            { 
              if (len == 0 && e.KeyChar != '0') 
              { 
                e.Handled = false; return; 
              } 
              else if (len == 0) 
              { 
                if (this.Flag == 1 && this.IsNetmask == false) 
                { 
                  return; 
                } 
              } 
              e.Handled = false; return; 
            } 
            else 
            { 
              // MessageBox.Show("编号最多只能输入3位数字!"); 
            } 
          } 
        } 
        else if (e.KeyChar == '.') 
        { 
          //MessageBox.Show("编号只能输入数字!"); 
          if (this.Text.Length != 0) 
          { 
            // 如果输入 . 就执行 TAB 键  
            SendKeys.SendWait("{TAB}"); 
          } 
        } 
        else if (this._isSendKey) 
        { 
          this.SelectAll(); 
        } 
 
      } 
 
 
      protected override void OnTextChanged(EventArgs e) 
      { 
 
        try 
        { 
          string currentStr = this.Text; 
          int currentNumber = Convert.ToInt32(currentStr); 
          this.Text = currentNumber.ToString(); 
          this.SelectionStart = currentStr.Length;//设置光标在末尾 
          if (_isNetmask == false)//表示ip地址 
          { 
 
            if (currentNumber > 223 || (currentNumber == 0 && this.Flag == 1)) 
            { 
              MessageBox.Show("你输入的" + currentStr + "不是有效数值,请指定一个介于1到223之间的数值!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
 
              if (this.Flag == 1 && currentNumber == 0) 
              { 
                this.Text = "1"; 
              } 
              else 
              { 
                this.Text = "223"; 
              } 
              _isSendKey = true; 
              this.Focus(); 
              this.SelectionStart = currentStr.Length;//设置光标在末尾 
              this.SelectAll(); 
            } 
            else 
            { 
              if (currentStr.Length == 3 && _isSendKey == false) 
              {// 当输入的字符个数为3时,跳入另外一个输入框 
                //MessageBox.Show("输入完毕"); 
                if (currentNumber == 0) 
                { 
                  this.Text = ""; 
                  MessageBox.Show("000"); 
                } 
                SendKeys.SendWait("{TAB}"); 
              } 
            } 
 
          } 
          else//子网掩码 
          { 
            if (currentNumber > 255) 
            { 
              MessageBox.Show("你输入的" + currentStr + "不是有效数值,请指定一个介于0到255之间的数值!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
              this.Text = "255"; 
              this.Focus(); 
              this.SelectionStart = currentStr.Length;//设置光标在末尾 
              this.SelectAll(); 
            } 
            else 
            { 
              if (currentStr.Length == 3 && _isSendKey == false) 
              {// 当输入的字符个数为3时,跳入另外一个输入框 
                //MessageBox.Show("输入完毕"); 
                SendKeys.SendWait("{TAB}"); 
 
              } 
            } 
          } 
        } 
        catch 
        { 
          // 异常处理 
        } 
      } 
 
      private void InitializeComponent() 
      { 
        this.SuspendLayout(); 
        //  
        // SubIpInputBox 
        //  
        this.TabIndexChanged += new System.EventHandler(this.SubTextBox_TabIndexChanged); 
        this.ResumeLayout(false); 
      } 
      private void SubTextBox_TabIndexChanged(object sender, EventArgs e) 
      { 
 
      } 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • 关于JavaScript中name的意义冲突示例介绍

    在昨天的《Javascript权威指南》学习笔记之十:ECMAScript 5 增强的对象模型一文中,对于一段代码的调试出现了一个奇怪现象,现将源代码贴在下面: 复制代码 代码如下: <script type="text/javascript"> function Person(){}...2014-05-31
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25