时间:2021-05-20
本文实例为大家分享了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
更多完整代码请点击下载:WinForm IP地址输入框控件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
场景:做一些网络设备应用时,需要有ip地址的输入,这就需要ip地址输入框控件思路:1重写TextBox为IpInputBox。2重写TextBox为SubIpI
前言在app的输入框中,需要应用到很多带有前缀说明的输入框,运用原有的输入框和文本控件,一个带头部的输入框就会增加三个控件在layout文件中。当布局文件输入框
用JavaScript实现的一个IP地址输入框,感觉不是太好,如果是192.168.1.1就必须输入192.168.001.001了别扭啊.a3{width:3
微信小程序input输入框控件今天主要详写一下微信小程序中的Input输入框控件,输入框在程序中是最常见的,登录,注册,获取搜索框中的内容等等都需要,同时,还需
在WinForm程序中,实现TextBox文本输入框占位符的方式也很多,最常用的是方式基于WindowsApiSendMessage函数发送EM_SETCUEB