欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WinForm IP地址输入框控件实现

程序员文章站 2023-12-18 13:37:46
本文实例为大家分享了winform ip地址输入框控件的具体实现代码,供大家参考,具体内容如下 ipinput.cs using system; using...

本文实例为大家分享了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地址输入框控件

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

上一篇:

下一篇: