WinForm IP地址輸入框控件實(shí)現(xiàn)
本文實(shí)例為大家分享了WinForm IP地址輸入框控件的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
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 + "不是有效項(xiàng)。請(qǐng)指定一個(gè)介于1和255之間的值。", "錯(cuò)誤", 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 + "不是有效項(xiàng)。請(qǐng)指定一個(gè)介于1和255之間的值。", "錯(cuò)誤", 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 + "不是有效項(xiàng)。請(qǐng)指定一個(gè)介于1和255之間的值。", "錯(cuò)誤", 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 + "不是有效項(xiàng)。請(qǐng)指定一個(gè)介于1和223之間的值。", "錯(cuò)誤", 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 + "不是有效項(xiàng)。請(qǐng)指定一個(gè)介于1和255之間的值。", "錯(cuò)誤", 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 + "不是有效項(xiàng)。請(qǐng)指定一個(gè)介于1和255之間的值。", "錯(cuò)誤", 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() + "不是有效項(xiàng)。請(qǐng)指定一個(gè)介于1和223之間的值。", "錯(cuò)誤", 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() + "不是有效項(xiàng)。請(qǐng)指定一個(gè)介于1和255之間的值。", "錯(cuò)誤", 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); } } }
更多完整代碼請(qǐng)點(diǎn)擊下載:WinForm IP地址輸入框控件
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C# 使用WPF 用MediaElement控件實(shí)現(xiàn)視頻循環(huán)
欄 目:C#教程
下一篇:C#中如何將MongoDB->RunCommand結(jié)果映射到業(yè)務(wù)類的方法總結(jié)
本文標(biāo)題:WinForm IP地址輸入框控件實(shí)現(xiàn)
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5191.html
您可能感興趣的文章
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法
- 01-10WinForm實(shí)現(xiàn)自定義右下角提示效果的方法
- 01-10.net2.0+ Winform項(xiàng)目實(shí)現(xiàn)彈出容器層
- 01-10winform 實(shí)現(xiàn)控制輸入法
- 01-10WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法
- 01-10Winform消除button按下出現(xiàn)的虛線簡(jiǎn)單實(shí)現(xiàn)方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子