WinForm IP地址输入框控件实现
本文实例为大家分享了WinFormIP地址输入框控件的具体实现代码,供大家参考,具体内容如下
IPInput.cs
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Drawing; usingSystem.Data; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Text.RegularExpressions; namespaceIPInputControl.Ctrl { publicpartialclassIPInput:UserControl { publicIPInput() { InitializeComponent(); } TextBoxParentTxt; privatevoidIPInput_Load(objectsender,EventArgse) { ParentTxt=txt_1; } publicvoidtxt_KeyDown(objectsender,KeyEventArgse) { 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(); } } elseif(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(); } } elseif(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(); } } elseif(ParentTxt.Text=="") { txt_3.Focus(); } break; } } elseif(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(); } } elseif(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(); } } elseif(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(); } } elseif(ParentTxt.Text=="") { txt_4.Focus(); } break; case"4": break; } } } publicvoidtxt_KeyPress(objectsender,KeyPressEventArgse) { ParentTxt=(TextBox)sender; Regexregex=newRegex(@"^[0-9]+$"); if(!regex.IsMatch(e.KeyChar.ToString())&&e.KeyChar!=(Char)Keys.Back) { e.Handled=true; } elseif(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; } } elseif(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; } } elseif(ParentTxt.Text.Length!=3) { e.Handled=false; } else { e.Handled=true; } break; } } } publicvoidtxt_TextChanged(objectsender,EventArgse) { 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
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Drawing; usingSystem.Data; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Text.RegularExpressions; namespaceIPInputControl.Ctrl { publicpartialclassControlText:TextBox { publicControlText() { InitializeComponent(); } publicvoidtxt_TextChange(objectsender,EventArgse) { if(this.Text.Length==3) { SendKeys.Send("{TAB}"); } } protectedoverrideboolProcessCmdKey(refMessagemsg,KeyskeyData) { if(keyData==Keys.Tab) { returntrue; } returnbase.ProcessCmdKey(refmsg,keyData); } } }
更多完整代码请点击下载:WinFormIP地址输入框控件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。