WinForm 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地址输入框控件

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

(0)

相关推荐

  • WinForm窗体间传值的方法

    本文实例讲述了WinForm窗体间传值的方法.分享给大家供大家参考.具体实现方法如下: 窗体间传递数据,无论是父窗体操作子窗体,还是子窗体操作符窗体,有以下几种方式:   1.公共静态变量: 2.使用共有属性: 3.使用委托与事件: 4.通过构造函数把主窗体传递到从窗体中: 一.通过静态变量 特点:传值是双向的,实现简单   实现代码如下: 在一个app类中定义一个静态成员value 复制代码 代码如下: public class app { public static string value

  • C# Winform实现导入和导出Excel文件

    本文实例为大家分享了Winform实现导入导出Excel文件的具体代码,供大家参考,具体内容如下 /// <summary> /// 导出Excel文件 /// </summary> /// /// <param name="dataSet"></param> /// <param name="dataTable">数据集</param> /// <param name="isS

  • C# winform程序实现开机自启动并且识别是开机启动还是双击启动

    开机启动程序,在很多场合都会用到,尤其是那种在后台运行的程序. 效果图: 以上两幅图都用到了命令行启动程序,为了模拟开机启动或者其他程序调用此程序. 第一幅图:程序启动可以根据不同参数,执行不同的操作.如果是双击启动,就自动运行逻辑代码,如果是带特定参数启动,就自动运行逻辑代码. 第二幅图:winform 程序设置开机启动,模拟双击启动和开机启动的不同效果. 开机启动并自动运行方法:其实思路很简单,就是将程序添加到注册表中,这设置值的时候,加一个参数就可以了.然后程序在入口函数处判断启动参数,如

  • c# WinForm 窗体之间传值的几种方式(小结)

    前言 小编最近维护一个Winfrom窗体,是项目中CS端的主窗体,很多子窗体需要从主窗体获取值,同时子窗体还需要给主窗体回传值,下面来给大家介绍一下. 正文 本文中以主窗体为frmMain,子窗体为frmGroup ,两窗体之间的传值来做示例. 方式一: 使用公共静态变量传值 主窗体frmMain中代码 public partial class frmMain : Form { //声明工位ID 为公共静态变量 public static string terminalID = "";

  • C#,winform,ShowDialog,子窗体向父窗体传值

    调用showdialog方法后,调用代码被暂停执行,等到调用showdialog方法的窗体关系后再继续执行.而且窗体可以返回一个dialogresult值,他描述了窗体关闭的原因,例如OK,Cancel,yes,no等.为了让窗体返回一个dialogresult,必须设置窗体的dialogresult值,或者在窗体的一个按钮上设置dialogresult属性. 例子: 下面是子窗体代码,要求输入phone,然后会返回给父窗体. using System; using System.Collect

  • WINFORM 窗体间的传值实现解析

    源码  https://github.com/SHAREVIEW/winform_valnue-from-form-2-form_20190919.git 在WinForm之间传值有很多种方法,在这里我用的是delegate and event进行传值. 新建一个WindowsApplication,创建两个WinForm.其实它们就是两个类. 每个WinForm中各加入一个Button和一个TextBox. 在WinForm2中写入代理和事件(delegate and event)如下: //

  • Winform窗体传值的方法(示例)

    进行用C#来开发windows应用程序项目时,不同窗口之间传值有很多种不同的方法, 假设项目中有两个窗体,Form1和Form2,要实现的效果是:点击Form1中的按钮打开From2,点击Form2中的按钮改变窗体Form1的背景颜色. 示例一: 点击Form1中的按钮打开From2,点击Form2中的按钮改变窗体Form1的背景颜色. Form1的代码: public partial class Form : Form { public Form() { InitializeComponent

  • WinForm 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.RegularE

  • WPF自定义实现IP地址输入控件

    一.前言 WPF没有内置IP地址输入控件,因此我们需要通过自己定义实现. 我们先看一下IP地址输入控件有什么特性: 输满三个数字焦点会往右移 键盘←→可以空光标移动 任意位置可复制整段IP地址,且支持x.x.x.x格式的粘贴赋值 删除字符会自动向左移动焦点 知道以上特性,我们就可以开始动手了. 二.构成 Grid+TextBox*4+TextBlock*3 通过这几个控件的组合,我们完成IP地址输入控件的功能. 界面代码如下: <UserControl x:Class="IpAddress

  • C#自定义IP输入框控件

    场景: 做一些网络设备应用时,需要有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 S

  • 微信小程序 input输入框控件详解及实例(多种示例)

    微信小程序 input输入框控件 今天主要详写一下微信小程序中的Input输入框控件,输入框在程序中是最常见的,登录,注册,获取搜索框中的内容等等都需要,同时,还需要设置不同样式的输入框,今天的代码中都要相应的使用. 首先主页面中将登录的样式进行了简单展示和使用, 代码如下: <!--index.wxml--> <!--如果在同一个form表单中创建了多个input输入框,可以给给每个输入框,创建自己的 name="userName"属性,可以区别哪个输入框,并通过添

  • WinForm实现移除控件某个事件的方法

    本文实例讲述了WinForm实现移除控件某个事件的方法,供大家参考借鉴一下.具体功能代码如下: 主要功能部分代码如下: /// <summary> /// 移除控件某个事件 /// </summary> /// <param name="control">控件</param> /// <param name="eventName">需要移除的控件名称eg:EventClick</param> p

  • WinForm中DataGridView折叠控件【超好看】

    刚到一家新公司,领导下发任务要用cs系统做一个表格折叠显示,这真是把我难倒了,自己工作6年一直以来都是做BS的系统.这如果在BS里面那太简单了,JqGrid默认都自带,可是DataGridview不支持折叠啊.自己一点经验没有,怎么办呢?于是上网搜了相关视频,资料,开始学习起来.最后借鉴源码封了这么一个东西,发出来分享下,也能让自己加深印象. 首先不多说,上图.如果大家感谢还不错,请继续往下阅读: 大概的效果就是这样. 上代码. 1.首先重写DataGridview,代码如下: public c

  • 用JavaScript实现的一个IP地址输入框

    用JavaScript实现的一个IP地址输入框,感觉不是太好,如果是192.168.1.1就必须输入192.168.001.001了别扭啊 .a3{width:30;border:0;text-align:center} function mask(obj){ obj.value=obj.value.replace(/[^\d]/g,'') key1=event.keyCode if (key1==37 || key1==39) { obj.blur(); nextip=parseInt(obj

  • C# WinForm实现窗体上控件自由拖动功能示例

    本文实例讲述了C# WinForm实现窗体上控件自由拖动功能.分享给大家供大家参考,具体如下: 说明:首先在窗体上放一个PictrueBox控件,命名为pb1,拖动完整代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; usin

  • C#之WinForm跨线程访问控件实例

    本文实例讲述了C#中WinForm跨线程访问控件的实现方法,分享给大家供大家参考. 具体实现方法如下: 1.跨线程访问控件委托和类的定义 复制代码 代码如下: using System; using System.Windows.Forms; namespace ahwildlife.Utils {     /// <summary>     /// 跨线程访问控件的委托     /// </summary>     public delegate void InvokeDeleg

  • Android 带清除功能的输入框控件实例详解

    Android 带清除功能的输入框控件实例详解 今天,看到一个很好的自定义输入框控件,于是记录一下. 效果很好: 一,自定义一个类,名为ClearEditText package com.example.clearedittext; import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatc

随机推荐