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地址輸入框控件
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#日期控件datetimepicker保存空值的三種方法
- winform dateTime數(shù)據(jù)類(lèi)型轉(zhuǎn)換方法
- c# Winform自定義控件-儀表盤(pán)功能
- Winform控件Picture實(shí)現(xiàn)圖片拖拽顯示效果
- WinForm實(shí)現(xiàn)鼠標(biāo)拖動(dòng)控件跟隨效果
- winform實(shí)現(xiàn)可拖動(dòng)的自定義Label控件
- 使用重繪項(xiàng)美化WinForm的控件
- C# winform自定義翻頁(yè)控件詳解
- C# WinForm實(shí)現(xiàn)窗體上控件自由拖動(dòng)功能示例
- Winform控件SaveFileDialog用于保存文件
- C#中WinForm控件的拖動(dòng)和縮放的實(shí)現(xiàn)代碼
- C# WinForm-Timer控件的使用
相關(guān)文章
如何在Mac系統(tǒng)使用Visual Studio Code運(yùn)行Python
這篇文章主要介紹了Mac使用Visual Studio Code運(yùn)行Python環(huán)境的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04C# 通過(guò)NI-VISA操作Tektronix TBS 2000B系列示波器的實(shí)現(xiàn)步驟
這篇文章主要介紹了C# 通過(guò)NI-VISA操作Tektronix TBS 2000B系列示波器的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02C#實(shí)現(xiàn)日期時(shí)間的格式化輸出的示例詳解
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)日期時(shí)間的格式化輸出的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-03-03基于WPF實(shí)現(xiàn)簡(jiǎn)單的文件夾比較工具
文件比較平常都是用Beyond?Compare,可以說(shuō)離不開(kāi)的神器,不過(guò)Beyond?Compare平常拿它主要是用來(lái)做代碼比較,用來(lái)做一些大批量的二進(jìn)制文件比較,其實(shí)有點(diǎn)不是很方便,所以本文來(lái)用WPF做一個(gè)簡(jiǎn)單的文件夾比較的小工具2023-05-05C#使用后臺(tái)線(xiàn)程BackgroundWorker處理任務(wù)的總結(jié)
這篇文章主要介紹了C#使用后臺(tái)線(xiàn)程BackgroundWorker處理任務(wù)的總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07