C#自定义IP输入框控件
程序员文章站
2023-12-18 14:09:40
场景: 做一些网络设备应用时,需要有ip地址的输入,这就需要ip地址输入框控件
思路:
1 重...
场景: 做一些网络设备应用时,需要有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 system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; namespace ip { public delegate void fallbackevent(ipinputbox box, int flag); public partial class dialog : form { public dialog() { initializecomponent(); } private void form1_load(object sender, eventargs e) { this.controls.add(ipbox); ipbox.location = new point(10,10); } private ipinputbox ipbox = new ipinputbox(false); } }
ipinputbox.cs 文件
using system; using system.collections.generic; using system.linq; using system.text; using system.windows.forms; using system.drawing; namespace ip { public class ipinputbox :textbox { private bool _isnetmask = false; public bool isnetmask { get { return _isnetmask; } set { _isnetmask = value; } } public ipinputbox(bool isnetmask) { _isnetmask = isnetmask; this.size = new system.drawing.size(150, 25); _dotlabel1.text = "."; _dotlabel2.text = "."; _dotlabel3.text = "."; _dotlabel1.size = new system.drawing.size(10, 25); _dotlabel2.size = new system.drawing.size(10, 25); _dotlabel3.size = new system.drawing.size(10, 25); _box1.isnetmask = _isnetmask; _box2.isnetmask = _isnetmask; _box3.isnetmask = _isnetmask; _box4.isnetmask = _isnetmask; _box1.flag = 1; _box2.flag = 2; _box3.flag = 3; _box4.flag = 4; this.controls.add(_box1); this.controls.add(_dotlabel1); this.controls.add(_box2); this.controls.add(_dotlabel2); this.controls.add(_box3); this.controls.add(_dotlabel3); this.controls.add(_box4); this.font = new system.drawing.font(this.font.name, 11); _box1.location = new system.drawing.point(-1, 2); _dotlabel1.location = new system.drawing.point(29, 2); _box2.location = new system.drawing.point(39, 2); _dotlabel2.location = new system.drawing.point(69, 2); _box3.location = new system.drawing.point(79, 2); _dotlabel3.location = new system.drawing.point(109, 2); _box4.location = new system.drawing.point(119, 2); _box1.box = this; _box2.box = this; _box3.box = this; _box4.box = this; } public void fallbackeventfun(ipinputbox box, int flag) { switch (flag) { case 1: _box1.focus(); break; case 2: _box1.focus(); break; case 3: _box2.focus(); ; break; case 4: _box3.focus(); ; break; } } private string _ipaddress = string.empty; public void updateipaddress() { try { string[] sarray = ipaddress.split(new char[] { '.' }); _box1.text = sarray[0]; _box2.text = sarray[1]; _box3.text = sarray[2]; _box4.text = sarray[3]; } catch (exception exp) { messagebox.show("更新字符串失败:" + exp.message); } } /// <summary> /// 获取ip地址 /// </summary> public string ipaddressstring { get { string _ipstr1 = _box1.text; string _ipstr2 = _box2.text; string _ipstr3 = _box3.text; string _ipstr4 = _box4.text; string _ipdotstr = "."; _ipaddress = _ipstr1 + _ipdotstr + _ipstr2 + _ipdotstr + _ipstr3 + _ipdotstr + _ipstr4; return _ipaddress; } set { _ipaddress = value; } } private string ipaddress = string.empty; public string ipaddress { get { return ipaddress; } set { ipaddress = value; } } private subipinputbox _box1 = new subipinputbox(""); private subipinputbox _box2 = new subipinputbox(""); private subipinputbox _box3 = new subipinputbox(""); private subipinputbox _box4 = new subipinputbox(""); private label _dotlabel1 = new label(); private label _dotlabel2 = new label(); private label _dotlabel3 = new label(); } }
subipinputbox.cs 文件
using system; using system.collections.generic; using system.linq; using system.text; using system.drawing; using system.windows.forms; namespace ip { public class subipinputbox:textbox { /// <summary> /// 判断是否是子网掩码,false表示ip地址,true表示子网掩码 /// </summary> private bool _isnetmask = false; private bool _issendkey = false; private system.componentmodel.icontainer components; private int _flag = 0; public int flag { get { return _flag; } set { _flag = value; } } /// <summary> /// /// </summary> public event fallbackevent textfallbackevent; //定义事件函数 public void fallbackeventfun(int flag) { if (textfallbackevent != null) { textfallbackevent(box, this.flag); } } public bool isnetmask { get { return _isnetmask; } set { _isnetmask = value; } } /// <summary> /// 构造函数 /// </summary> public subipinputbox(bool isnetmask) { _isnetmask = isnetmask; box = new ipinputbox(_isnetmask); this.font = new system.drawing.font(this.font.name, 11); this.borderstyle = system.windows.forms.borderstyle.none;//去掉边框 this.textalign = horizontalalignment.center;//字体居中 this.size = new system.drawing.size(30, 25); this.maxlength = 3; } public subipinputbox(string str) { this.text = str; this.size = new system.drawing.size(30, 25); this.maxlength = 3; this.borderstyle = system.windows.forms.borderstyle.none;//去掉边框 this.textalign = horizontalalignment.center;//字体居中 } private ipinputbox box; public ipinputbox box { get { return box; } set { box = value; } } protected override void onkeyup(keyeventargs e) { base.onkeyup(e); if (this.text == "") { if (e.keycode.tostring() == "back") { this.textfallbackevent += new fallbackevent(box.fallbackeventfun); this.fallbackeventfun(this.flag); } } } protected override void onkeypress(keypresseventargs e) { base.onkeypress(e); //阻止从键盘输入键 e.handled = true; if ((e.keychar >= '0' && e.keychar <= '9') || (e.keychar == (char)8)) { if ((e.keychar == (char)8)) { e.handled = false; return; } else { int len = this.text.length; if (len < 4) { if (len == 0 && e.keychar != '0') { e.handled = false; return; } else if (len == 0) { if (this.flag == 1 && this.isnetmask == false) { return; } } e.handled = false; return; } else { // messagebox.show("编号最多只能输入3位数字!"); } } } else if (e.keychar == '.') { //messagebox.show("编号只能输入数字!"); if (this.text.length != 0) { // 如果输入 . 就执行 tab 键 sendkeys.sendwait("{tab}"); } } else if (this._issendkey) { this.selectall(); } } protected override void ontextchanged(eventargs e) { try { string currentstr = this.text; int currentnumber = convert.toint32(currentstr); this.text = currentnumber.tostring(); this.selectionstart = currentstr.length;//设置光标在末尾 if (_isnetmask == false)//表示ip地址 { if (currentnumber > 223 || (currentnumber == 0 && this.flag == 1)) { messagebox.show("你输入的" + currentstr + "不是有效数值,请指定一个介于1到223之间的数值!", "错误", messageboxbuttons.ok, messageboxicon.warning); if (this.flag == 1 && currentnumber == 0) { this.text = "1"; } else { this.text = "223"; } _issendkey = true; this.focus(); this.selectionstart = currentstr.length;//设置光标在末尾 this.selectall(); } else { if (currentstr.length == 3 && _issendkey == false) {// 当输入的字符个数为3时,跳入另外一个输入框 //messagebox.show("输入完毕"); if (currentnumber == 0) { this.text = ""; messagebox.show("000"); } sendkeys.sendwait("{tab}"); } } } else//子网掩码 { if (currentnumber > 255) { messagebox.show("你输入的" + currentstr + "不是有效数值,请指定一个介于0到255之间的数值!", "错误", messageboxbuttons.ok, messageboxicon.warning); this.text = "255"; this.focus(); this.selectionstart = currentstr.length;//设置光标在末尾 this.selectall(); } else { if (currentstr.length == 3 && _issendkey == false) {// 当输入的字符个数为3时,跳入另外一个输入框 //messagebox.show("输入完毕"); sendkeys.sendwait("{tab}"); } } } } catch { // 异常处理 } } private void initializecomponent() { this.suspendlayout(); // // subipinputbox // this.tabindexchanged += new system.eventhandler(this.subtextbox_tabindexchanged); this.resumelayout(false); } private void subtextbox_tabindexchanged(object sender, eventargs e) { } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。