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地址输入框控件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
上一篇:C# 使用WPF 用MediaElement控件实现视频循环播放
栏 目:C#教程
下一篇:C#中如何将MongoDB->RunCommand结果映射到业务类的方法总结
本文标题:WinForm IP地址输入框控件实现
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5191.html
您可能感兴趣的文章
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10WinForm实现仿视频播放器左下角滚动新闻效果的方法
- 01-10C#实现Winform中打开网页页面的方法
- 01-10winform实现创建最前端窗体的方法
- 01-10WinForm实现自定义右下角提示效果的方法
- 01-10.net2.0+ Winform项目实现弹出容器层
- 01-10winform 实现控制输入法
- 01-10WinForm实现程序一段时间不运行自动关闭的方法
- 01-10Winform消除button按下出现的虚线简单实现方法
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10delphi制作wav文件的方法
- 01-11ajax实现页面的局部加载
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10C#中split用法实例总结
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 04-02jquery与jsp,用jquery
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10SublimeText编译C开发环境设置
- 01-10使用C语言求解扑克牌的顺子及n个骰子