亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

WinForm IP地址輸入框控件實現

 更新時間:2018年05月08日 11:53:37   作者:zf15256888839  
這篇文章主要為大家詳細介紹了WinForm IP地址輸入框控件的實現代碼,基于VS2010模擬windows系統(tǒng)自帶IP輸入框,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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地址輸入框控件

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

相關文章

  • 如何在Mac系統(tǒng)使用Visual Studio Code運行Python

    如何在Mac系統(tǒng)使用Visual Studio Code運行Python

    這篇文章主要介紹了Mac使用Visual Studio Code運行Python環(huán)境的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • C# 通過NI-VISA操作Tektronix TBS 2000B系列示波器的實現步驟

    C# 通過NI-VISA操作Tektronix TBS 2000B系列示波器的實現步驟

    這篇文章主要介紹了C# 通過NI-VISA操作Tektronix TBS 2000B系列示波器的實現步驟,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下
    2021-02-02
  • 手動編譯C#代碼的方法

    手動編譯C#代碼的方法

    在本文里小編給大家分享的是關于手動編譯C#代碼的方法和步驟,對此有需要的朋友們可以學習下。
    2018-12-12
  • C#編寫一個簡單記事本功能

    C#編寫一個簡單記事本功能

    這篇文章主要為大家詳細介紹了C#編寫一個簡單記事本功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • C#讀取CSV文件的方法總結

    C#讀取CSV文件的方法總結

    CSV文件是一種簡單的文本文件格式,用于存儲表格數據,在C#中,有多種方法可以用于讀取CSV文件,本文將介紹幾種常見的讀取CSV文件的方法,包括使用System.IO命名空間中的類、使用CsvHelper庫以及使用LINQ,需要的朋友可以參考下
    2024-05-05
  • C#實現日期時間的格式化輸出的示例詳解

    C#實現日期時間的格式化輸出的示例詳解

    這篇文章主要為大家詳細介紹了C#實現日期時間的格式化輸出的相關資料,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下
    2023-03-03
  • 基于WPF實現簡單的文件夾比較工具

    基于WPF實現簡單的文件夾比較工具

    文件比較平常都是用Beyond?Compare,可以說離不開的神器,不過Beyond?Compare平常拿它主要是用來做代碼比較,用來做一些大批量的二進制文件比較,其實有點不是很方便,所以本文來用WPF做一個簡單的文件夾比較的小工具
    2023-05-05
  • C#使用后臺線程BackgroundWorker處理任務的總結

    C#使用后臺線程BackgroundWorker處理任務的總結

    這篇文章主要介紹了C#使用后臺線程BackgroundWorker處理任務的總結,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • c# this關鍵字用法代碼詳解

    c# this關鍵字用法代碼詳解

    在本篇文章里小編給大家整理的是關于c# this關鍵字用法以及相關實例代碼,有興趣的朋友們可以學習下。
    2020-02-02
  • c# TreeView添加右鍵快鍵菜單有兩種方法

    c# TreeView添加右鍵快鍵菜單有兩種方法

    c# TreeView添加右鍵快鍵菜單有兩種方法,需要的朋友可以參考一下
    2013-04-04

最新評論