這篇文章主要介紹C#如何自定義IP輸入框控件,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
十載的開原網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營(yíng)銷網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整開原建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“開原網(wǎng)站設(shè)計(jì)”,“開原網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
場(chǎng)景: 做一些網(wǎng)絡(luò)設(shè)備應(yīng)用時(shí),需要有ip地址的輸入,這就需要ip地址輸入框控件
思路:
1 重寫TextBox 為IpInputBox。
2 重寫TextBox為SubIpInputBox
3 一個(gè)IpInputBox 添加4個(gè)SubIpInputBox 和3個(gè)Label
控件圖:

具體代碼:
主窗口文件 Dialog.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace IP
{
public delegate void FallBackEvent(IpInputBox box, int flag);
public partial class Dialog : Form
{
public Dialog()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Controls.Add(ipBox);
ipBox.Location = new Point(10,10);
}
private IpInputBox ipBox = new IpInputBox(false);
}
}IpInputBox.cs 文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace IP
{
public class IpInputBox :TextBox
{
private bool _isNetmask = false;
public bool IsNetmask
{
get { return _isNetmask; }
set { _isNetmask = value; }
}
public IpInputBox(bool isNetmask)
{
_isNetmask = isNetmask;
this.Size = new System.Drawing.Size(150, 25);
_dotLabel1.Text = ".";
_dotLabel2.Text = ".";
_dotLabel3.Text = ".";
_dotLabel1.Size = new System.Drawing.Size(10, 25);
_dotLabel2.Size = new System.Drawing.Size(10, 25);
_dotLabel3.Size = new System.Drawing.Size(10, 25);
_box1.IsNetmask = _isNetmask;
_box2.IsNetmask = _isNetmask;
_box3.IsNetmask = _isNetmask;
_box4.IsNetmask = _isNetmask;
_box1.Flag = 1;
_box2.Flag = 2;
_box3.Flag = 3;
_box4.Flag = 4;
this.Controls.Add(_box1);
this.Controls.Add(_dotLabel1);
this.Controls.Add(_box2);
this.Controls.Add(_dotLabel2);
this.Controls.Add(_box3);
this.Controls.Add(_dotLabel3);
this.Controls.Add(_box4);
this.Font = new System.Drawing.Font(this.Font.Name, 11);
_box1.Location = new System.Drawing.Point(-1, 2);
_dotLabel1.Location = new System.Drawing.Point(29, 2);
_box2.Location = new System.Drawing.Point(39, 2);
_dotLabel2.Location = new System.Drawing.Point(69, 2);
_box3.Location = new System.Drawing.Point(79, 2);
_dotLabel3.Location = new System.Drawing.Point(109, 2);
_box4.Location = new System.Drawing.Point(119, 2);
_box1.Box = this;
_box2.Box = this;
_box3.Box = this;
_box4.Box = this;
}
public void FallBackEventFun(IpInputBox box, int flag)
{
switch (flag)
{
case 1:
_box1.Focus();
break;
case 2:
_box1.Focus();
break;
case 3:
_box2.Focus(); ;
break;
case 4:
_box3.Focus(); ;
break;
}
}
private string _ipAddress = string.Empty;
public void UpDateIpaddress()
{
try
{
string[] sArray = ipAddress.Split(new char[] { '.' });
_box1.Text = sArray[0];
_box2.Text = sArray[1];
_box3.Text = sArray[2];
_box4.Text = sArray[3];
}
catch (Exception exp)
{
MessageBox.Show("更新字符串失敗:" + exp.Message);
}
}
/// <summary>
/// 獲取ip地址
/// </summary>
public string IpAddressString
{
get
{
string _ipStr1 = _box1.Text;
string _ipStr2 = _box2.Text;
string _ipStr3 = _box3.Text;
string _ipStr4 = _box4.Text;
string _ipDotStr = ".";
_ipAddress = _ipStr1 + _ipDotStr + _ipStr2 + _ipDotStr + _ipStr3 + _ipDotStr + _ipStr4;
return _ipAddress;
}
set
{
_ipAddress = value;
}
}
private string ipAddress = string.Empty;
public string IpAddress
{
get { return ipAddress; }
set { ipAddress = value; }
}
private SubIpInputBox _box1 = new SubIpInputBox("");
private SubIpInputBox _box2 = new SubIpInputBox("");
private SubIpInputBox _box3 = new SubIpInputBox("");
private SubIpInputBox _box4 = new SubIpInputBox("");
private Label _dotLabel1 = new Label();
private Label _dotLabel2 = new Label();
private Label _dotLabel3 = new Label();
}
}SubIpInputBox.cs 文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace IP
{
public class SubIpInputBox:TextBox
{
/// <summary>
/// 判斷是否是子網(wǎng)掩碼,false表示ip地址,true表示子網(wǎng)掩碼
/// </summary>
private bool _isNetmask = false;
private bool _isSendKey = false;
private System.ComponentModel.IContainer components;
private int _flag = 0;
public int Flag
{
get { return _flag; }
set { _flag = value; }
}
/// <summary>
///
/// </summary>
public event FallBackEvent TextFallBackEvent;
//定義事件函數(shù)
public void FallBackEventFun(int flag)
{
if (TextFallBackEvent != null)
{
TextFallBackEvent(box, this.Flag);
}
}
public bool IsNetmask
{
get { return _isNetmask; }
set { _isNetmask = value; }
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public SubIpInputBox(bool isNetmask)
{
_isNetmask = isNetmask;
box = new IpInputBox(_isNetmask);
this.Font = new System.Drawing.Font(this.Font.Name, 11);
this.BorderStyle = System.Windows.Forms.BorderStyle.None;//去掉邊框
this.TextAlign = HorizontalAlignment.Center;//字體居中
this.Size = new System.Drawing.Size(30, 25);
this.MaxLength = 3;
}
public SubIpInputBox(string str)
{
this.Text = str;
this.Size = new System.Drawing.Size(30, 25);
this.MaxLength = 3;
this.BorderStyle = System.Windows.Forms.BorderStyle.None;//去掉邊框
this.TextAlign = HorizontalAlignment.Center;//字體居中
}
private IpInputBox box;
public IpInputBox Box
{
get { return box; }
set { box = value; }
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
if (this.Text == "")
{
if (e.KeyCode.ToString() == "Back")
{
this.TextFallBackEvent += new FallBackEvent(box.FallBackEventFun);
this.FallBackEventFun(this.Flag);
}
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
//阻止從鍵盤輸入鍵
e.Handled = true;
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == (char)8))
{
if ((e.KeyChar == (char)8))
{
e.Handled = false; return;
}
else
{
int len = this.Text.Length;
if (len < 4)
{
if (len == 0 && e.KeyChar != '0')
{
e.Handled = false; return;
}
else if (len == 0)
{
if (this.Flag == 1 && this.IsNetmask == false)
{
return;
}
}
e.Handled = false; return;
}
else
{
// MessageBox.Show("編號(hào)最多只能輸入3位數(shù)字!");
}
}
}
else if (e.KeyChar == '.')
{
//MessageBox.Show("編號(hào)只能輸入數(shù)字!");
if (this.Text.Length != 0)
{
// 如果輸入 . 就執(zhí)行 TAB 鍵
SendKeys.SendWait("{TAB}");
}
}
else if (this._isSendKey)
{
this.SelectAll();
}
}
protected override void OnTextChanged(EventArgs e)
{
try
{
string currentStr = this.Text;
int currentNumber = Convert.ToInt32(currentStr);
this.Text = currentNumber.ToString();
this.SelectionStart = currentStr.Length;//設(shè)置光標(biāo)在末尾
if (_isNetmask == false)//表示ip地址
{
if (currentNumber > 223 || (currentNumber == 0 && this.Flag == 1))
{
MessageBox.Show("你輸入的" + currentStr + "不是有效數(shù)值,請(qǐng)指定一個(gè)介于1到223之間的數(shù)值!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (this.Flag == 1 && currentNumber == 0)
{
this.Text = "1";
}
else
{
this.Text = "223";
}
_isSendKey = true;
this.Focus();
this.SelectionStart = currentStr.Length;//設(shè)置光標(biāo)在末尾
this.SelectAll();
}
else
{
if (currentStr.Length == 3 && _isSendKey == false)
{// 當(dāng)輸入的字符個(gè)數(shù)為3時(shí),跳入另外一個(gè)輸入框
//MessageBox.Show("輸入完畢");
if (currentNumber == 0)
{
this.Text = "";
MessageBox.Show("000");
}
SendKeys.SendWait("{TAB}");
}
}
}
else//子網(wǎng)掩碼
{
if (currentNumber > 255)
{
MessageBox.Show("你輸入的" + currentStr + "不是有效數(shù)值,請(qǐng)指定一個(gè)介于0到255之間的數(shù)值!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.Text = "255";
this.Focus();
this.SelectionStart = currentStr.Length;//設(shè)置光標(biāo)在末尾
this.SelectAll();
}
else
{
if (currentStr.Length == 3 && _isSendKey == false)
{// 當(dāng)輸入的字符個(gè)數(shù)為3時(shí),跳入另外一個(gè)輸入框
//MessageBox.Show("輸入完畢");
SendKeys.SendWait("{TAB}");
}
}
}
}
catch
{
// 異常處理
}
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// SubIpInputBox
//
this.TabIndexChanged += new System.EventHandler(this.SubTextBox_TabIndexChanged);
this.ResumeLayout(false);
}
private void SubTextBox_TabIndexChanged(object sender, EventArgs e)
{
}
}
}以上是“C#如何自定義IP輸入框控件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
名稱欄目:C#如何自定義IP輸入框控件
本文URL:http://chinadenli.net/article36/jhpssg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、App開發(fā)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站排名、手機(jī)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)