a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

WinForm IP地址輸入框控件實現

2019-10-29 21:02:25
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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

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); } }}

更多完整代碼請點擊下載:WinForm IP地址輸入框控件

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩欧美在线播放 | 特级毛片在线 | 九九亚洲 | 日本免费一区二区视频 | 99热免费在线 | 中文字幕在线观看精品视频 | 91免费版在线观看 | 99爱在线观看 | 欧美综合在线观看 | 成人精品久久 | 久久精品国产99 | 国内精品国产成人国产三级粉色 | 久久久久久久亚洲精品 | 欧美精品一区二区三区在线四季 | 国产成人精品午夜在线播放 | 91在线成人 | 精品超碰 | 国产精品自拍视频 | 国产精品美女一区二区三区四区 | 国产在线精品一区 | 久久av一区二区三区亚洲 | 日韩精品一区二区三区老鸭窝 | 免费激情小视频 | 成人午夜sm精品久久久久久久 | 国产日韩欧美一区二区在线观看 | 日日人人 | 久久九九 | 亚洲男人天堂网 | 日韩久久久精品 | 成年人在线看 | 欧美日韩久久精品 | 成人av影片 | 日本阿v视频高清在线中文 日本在线观看 | 国产精品入口久久 | 亚洲精品久久久 | 亚洲日韩视频免费观看 | 亚洲精品成人免费 | 免费黄色小视频 | 久久99精品久久久久 | 中文日韩在线 | 国产中文在线播放 |