WinForm IP地址输入框控件实现

 更新时间:2018年05月08日 11:53:37   作者:zf15256888839  
这篇文章主要为大家详细介绍了WinForm IP地址输入框控件的实现代码,基于VS2010模拟windows系统自带IP输入框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了WinForm IP地址输入框控件的具体实现代码,供大家参考,具体内容如下

IPInput.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace IPInputControl.Ctrl
{
 public partial class IPInput : UserControl
 {
 public IPInput()
 {
  InitializeComponent();
 }
 TextBox ParentTxt;
 private void IPInput_Load(object sender, EventArgs e)
 {
  ParentTxt = txt_1;
 }
 public void txt_KeyDown(object sender, KeyEventArgs e)
 {
  ParentTxt = (TextBox)sender;
  if (e.KeyCode == Keys.Left)
  {
  switch (ParentTxt.Name.Split('_')[1])
  {
   case "1":
   break;
   case "2":
   if (ParentTxt.SelectionStart == 0 && ParentTxt.Text != "")
   {
    if (int.Parse(ParentTxt.Text) > 255)
    {
    MessageBox.Show(ParentTxt.Text + "不是有效项。请指定一个介于1和255之间的值。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    ParentTxt.Text = "255";
    ParentTxt.SelectionStart = 0;
    }
    else
    {
    txt_1.Focus();
    }
   }
   else if (ParentTxt.Text == "")
   {
    txt_1.Focus();
   }
   break;
   case "3":
   if (ParentTxt.SelectionStart == 0 && ParentTxt.Text != "")
   {
    if (int.Parse(ParentTxt.Text) > 255)
    {
    MessageBox.Show(ParentTxt.Text + "不是有效项。请指定一个介于1和255之间的值。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    ParentTxt.Text = "255";
    ParentTxt.SelectionStart = 0;
    }
    else
    {
    txt_2.Focus();
    }
   }
   else if (ParentTxt.Text == "")
   {
    txt_2.Focus();
   }
   break;
   case "4":
   if (ParentTxt.SelectionStart == 0 && ParentTxt.Text != "")
   {
    if (int.Parse(ParentTxt.Text) > 255)
    {
    MessageBox.Show(ParentTxt.Text + "不是有效项。请指定一个介于1和255之间的值。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    ParentTxt.Text = "255";
    ParentTxt.SelectionStart = 0;
    }
    else
    {
    txt_3.Focus();
    }
   }
   else if (ParentTxt.Text == "")
   {
    txt_3.Focus();
   }
   break;
  }
  }
  else if (e.KeyCode == Keys.Right)
  {
  switch (ParentTxt.Name.Split('_')[1])
  {
   case "1":
   if (ParentTxt.SelectionStart == ParentTxt.Text.Length && ParentTxt.Text != "")
   {
    if (int.Parse(ParentTxt.Text) > 223)
    {
    MessageBox.Show(ParentTxt.Text + "不是有效项。请指定一个介于1和223之间的值。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    ParentTxt.Text = "223";
    ParentTxt.SelectionStart = ParentTxt.Text.Length;
    }
    else
    {
    txt_2.Focus();
    }
   }
   else if (ParentTxt.Text == "")
   {
    txt_2.Focus();
   }
   break;
   case "2":
   if (ParentTxt.SelectionStart == ParentTxt.Text.Length && ParentTxt.Text != "")
   {
    if (int.Parse(ParentTxt.Text) > 255)
    {
    MessageBox.Show(ParentTxt.Text + "不是有效项。请指定一个介于1和255之间的值。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    ParentTxt.Text = "255";
    ParentTxt.SelectionStart = ParentTxt.Text.Length;
    }
    else
    {
    txt_3.Focus();
    }
   }
   else if (ParentTxt.Text == "")
   {
    txt_3.Focus();
   }
   break;
   case "3":
   if (ParentTxt.SelectionStart == ParentTxt.Text.Length && ParentTxt.Text != "")
   {
    if (int.Parse(ParentTxt.Text) > 255)
    {
    MessageBox.Show(ParentTxt.Text + "不是有效项。请指定一个介于1和255之间的值。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    ParentTxt.Text = "255";
    ParentTxt.SelectionStart = ParentTxt.Text.Length;
    }
    else
    {
    txt_4.Focus();
    }
   }
   else if (ParentTxt.Text == "")
   {
    txt_4.Focus();
   }
   break;
   case "4":
   break;
  }
  }
 }
 public void txt_KeyPress(object sender, KeyPressEventArgs e)
 {
  ParentTxt = (TextBox)sender;
  Regex regex = new Regex(@"^[0-9]+$");
  if (!regex.IsMatch(e.KeyChar.ToString()) && e.KeyChar != (Char)Keys.Back)
  {
  e.Handled = true;
  }
  else if (e.KeyChar == (Char)Keys.Back)
  {
  e.Handled = false;
  switch (ParentTxt.Name.Split('_')[1])
  {
   case "1":
   break;
   case "2":
   if (ParentTxt.SelectionStart == 0)
   {
    txt_1.Focus();
    if (txt_1.Text != "")
    {
    txt_1.Text = txt_1.Text.Substring(0, txt_1.Text.Length - 1);
    }
    txt_1.SelectionStart = txt_1.Text.Length;
   }
   break;
   case "3":
   if (ParentTxt.SelectionStart == 0)
   {
    txt_2.Focus();
    if (txt_2.Text != "")
    {
    txt_2.Text = txt_2.Text.Substring(0, txt_2.Text.Length - 1);
    }
    txt_2.SelectionStart = txt_2.Text.Length;
   }
   break;
   case "4":
   if (ParentTxt.SelectionStart == 0)
   {
    txt_3.Focus();
    if (txt_3.Text != "")
    {
    txt_3.Text = txt_3.Text.Substring(0, txt_3.Text.Length - 1);
    }
    txt_3.SelectionStart = txt_3.Text.Length;
   }
   break;
  }
  }
  else
  {
  switch (ParentTxt.Name.Split('_')[1])
  {
   case "1":
   if (ParentTxt.SelectionStart == ParentTxt.Text.Length)
   {
    if (int.Parse(ParentTxt.Text + e.KeyChar.ToString()) > 223)
    {
    MessageBox.Show(ParentTxt.Text + e.KeyChar.ToString() + "不是有效项。请指定一个介于1和223之间的值。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    e.Handled = true;
    ParentTxt.Text = "223";
    }
    else
    {
    e.Handled = false;
    }
   }
   else if(ParentTxt.Text.Length != 3)
   {
    e.Handled = false;
   }
   else
   {
    e.Handled = true;
   }
   break;
   default:
   if (ParentTxt.SelectionStart == ParentTxt.Text.Length)
   {
    if (int.Parse(ParentTxt.Text + e.KeyChar.ToString()) > 255)
    {
    MessageBox.Show(ParentTxt.Text + e.KeyChar.ToString() + "不是有效项。请指定一个介于1和255之间的值。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    e.Handled = true;
    ParentTxt.Text = "255";
    }
    else
    {
    e.Handled = false;
    }
   }
   else if (ParentTxt.Text.Length != 3)
   {
    e.Handled = false;
   }
   else
   {
    e.Handled = true;
   }
   break;
  }
  }
 }
 public void txt_TextChanged(object sender, EventArgs e)
 {
  if (ParentTxt.Text.Length == 3)
  {
  switch (ParentTxt.Name.Split('_')[1])
  {
   case "1":
   if (ParentTxt.SelectionStart == ParentTxt.Text.Length)
   {
    txt_2.Focus();
   }
   break;
   case "2":
   if (ParentTxt.SelectionStart == ParentTxt.Text.Length)
   {
    txt_3.Focus();
   }
   break;
   case "3":
   if (ParentTxt.SelectionStart == ParentTxt.Text.Length)
   {
    txt_4.Focus();
   }
   break;
   case "4":
   break;
  }
  }
 }
 }
}

ControlText.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace IPInputControl.Ctrl
{
 public partial class ControlText : TextBox
 {
 public ControlText()
 {
  InitializeComponent();
 }
 public void txt_TextChange(object sender, EventArgs e)
 {
  if (this.Text.Length == 3)
  {
  SendKeys.Send("{TAB}");
  }
 }
 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
 {
  if (keyData == Keys.Tab)
  {
  return true;
  }
  return base.ProcessCmdKey(ref msg, keyData);
 }
 }
}

更多完整代码请点击下载:WinForm IP地址输入框控件

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

相关文章

  • C#图片查看器实现方法

    C#图片查看器实现方法

    本篇文章给大家分享了用C#制作图片查看器的方法以及先实现代码,有需要的读者们参考下。
    2018-03-03
  • C#编程实现自定义热键的方法

    C#编程实现自定义热键的方法

    这篇文章主要介绍了C#编程实现自定义热键的方法,涉及C#键盘按键设置的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • 给c#添加SetTimeout和SetInterval函数

    给c#添加SetTimeout和SetInterval函数

    Javascript中的SetTimeout和SetInterval函数很方便,把他们移植到c#中来。
    2008-03-03
  • WPF实现魔方小游戏

    WPF实现魔方小游戏

    这篇文章主要为大家详细介绍了WPF实现魔方小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • 举例讲解C#编程中对设计模式中的单例模式的运用

    举例讲解C#编程中对设计模式中的单例模式的运用

    这篇文章主要介绍了C#编程中对设计模式中的单例模式的运用,单例模式在.NET框架的相关开发中也被经常用到,需要的朋友可以参考下
    2016-02-02
  • C#调用python文件执行

    C#调用python文件执行

    这篇文章主要为大家详细介绍了C#调用python文件执行的相关方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 基于C#解决库存扣减及订单创建时防止并发死锁的问题

    基于C#解决库存扣减及订单创建时防止并发死锁的问题

    这篇文章主要介绍了基于C#解决库存扣减及订单创建时防止并发死锁的问题,很多开发人员对于这个问题的排查起来是比较困难的,而生产生的原因多种多样,很多人认是因为表中的数据太多了同时操作的人多人才会产生这种错误,下面我们来还原一下死锁的过程
    2022-05-05
  • C#使用String和StringBuilder运行速度测试及各自常用方法简介

    C#使用String和StringBuilder运行速度测试及各自常用方法简介

    今天小编就为大家分享一篇关于C#使用String和StringBuilder运行速度测试及各自常用方法简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • C# Socket通信的实现(同时监听多客户端)

    C# Socket通信的实现(同时监听多客户端)

    这篇文章主要介绍了C# Socket通信的实现(同时监听多客户端),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • winform多线程组件BackgroundWorker使用

    winform多线程组件BackgroundWorker使用

    这篇文章介绍了winform多线程组件BackgroundWorker的使用方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05

最新评论