1.创建winform项目
2.将Form1.cs重命名为Dialog,将Form1窗体属性中的Name更名为Dialog
3.创建类-IpInputBox.cs
4.创建类-SubIpInptBox.cs
SubIpInptBox.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace IP
{
public class SubIpInputBox : TextBox
{
private bool _isNetmask = false;
private bool _isSendkey = false;
private IpInputBox box;public IpInputBox Box//?
{
get { return box; }
set { box = value; }
}
private System.ComponentModel.IContainer components;//提供容器的功能
private int _flag = 0;
public event FallBackEvent TextFallBackEvent;//定义事件函数,此函数为委托函数public int Flag//属性,判断光标在哪一个小的Textbox中
{
get { return _flag; }
set { _flag = value; }
}public bool IsNetmask//属性,判断是ip还是子网掩码
{
get { return _isNetmask; }
set { _isNetmask = value; }
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="isNetmask"></param>
public SubIpInputBox(bool isNetmask)
{
_isNetmask = isNetmask;
box = new IpInputBox(_isNetmask);
this.BorderStyle = System.Windows.Forms.BorderStyle.None;//去边框
this.TextAlign = HorizontalAlignment.Center;//字体居中
this.Size = new System.Drawing.Size(30, 25);
this.MaxLength = 3;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="str"></param>
public SubIpInputBox(string str)
{
this.Text = str;
this.Size = new System.Drawing.Size(30, 35);
this.MaxLength = 3;
this.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.TextAlign = HorizontalAlignment.Center;
}
/// <summary>
/// 事件委托函数
/// </summary>
/// <param name="flag"></param>
public void FallBackEventFun(int flag)
{
if (TextFallBackEvent != null)
{
TextFallBackEvent(box, this.Flag);
}
}
/// <summary>
/// //释放键盘时执行 ?这里涉及flag的值,需要再处理?
/// </summary>
/// <param name="e"></param>
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);
}
}
}
/// <summary>
/// 可判断按下的是否只是数字或者退格,ip是否大于223,子网掩码是否大于255
/// </summary>
/// <param name="e"></param>
protected override void OnKeyPress(KeyPressEventArgs e)//键盘按下时执行事件
{
base.OnKeyPress(e);
e.Handled = true;//无效化刚刚从键盘输入的值,键盘只能输入0-9的数字和退格键
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == (char)8))
{
if ((e.KeyChar == (char)8))//判断输入的是否是退格键ASCLL码值
{
e.Handled = false;
return;
}
else//0-9
{
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("IP地址的每位只能輸入3位數字");
}
}//else0-9
}//if0-9或者退格
else if(e.KeyChar=='.')
{
if (this.Text.Length != 0)
{
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;//将光标设置到末尾
if (_isNetmask == false)//表示ip地址
{
if (currentNumber > 223 || (currentNumber == 0 && this.Flag == 1))
{
MessageBox.Show("你輸入的" + currentStr + "不是有效數字,請指定一個介於1-233之間的數值", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);//MessageBoxIcon 消息框附加一个警告符
if (this.Flag == 1 && currentNumber == 0)
{
this.Text = "1";}
else
{
this.Text = "233";
}
_isSendkey = true;
this.Focus();
this.SelectionStart = currentStr.Length;
this.SelectAll();
}//if currentNumber>233
else
{
if (currentStr.Length == 3 && _isSendkey == false)//当输入的字符数大于3时,跳入到另一个输入框
{
if (currentNumber == 0)
{
this.Text = "";
}
SendKeys.SendWait("{TAB}");
}
}//else currentNumber<233}//if isNetmask==false
else//子网掩码
{
if (currentNumber > 255)
{
MessageBox.Show("你輸入的" + currentStr + "不是有效数值,请指定一个介于0到255之间的数值!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.Text = "255";
this.Focus();
this.SelectionStart = currentStr.Length;
this.SelectAll();
}
else
{
if (currentStr.Length == 3 && _isSendkey == false)
{
SendKeys.SendWait("{TAB}");
}
}
}
}
catch
{ }
}
private void InitializeComponent()
{
this.TabIndexChanged += new System.EventHandler(this.SubTextBox_TabIndexChanged);
this.ResumeLayout(false);
}
private void SubTextBox_TabIndexChanged(object sender, EventArgs e)
{}
}
}
IpInputBox.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace IP
{
public class IpInputBox : TextBox
{
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();
private string _ipAddress = string.Empty;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 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.Flag = 1;
_box2.Flag = 2;
_box3.Flag = 3;
_box4.Flag = 4;//ip格式
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:
_box2.Focus();
break;
case 3:
_box3.Focus();
break;
case 4:
_box4.Focus();
break;
}
}
public void upDataIpaddress()
{
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);
}}
}
}
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
{
private IpInputBox ipBox = new IpInputBox(false);
public Dialog()
{
InitializeComponent();
}
private void Dialog_Load(object sender, EventArgs e)
{
this.Controls.Add(ipBox);
ipBox.Location = new Point(10,10);
}}
}
最后检查一下Form1.Designer.cs中的函数InitializeComponent有没有下面这行代码
this.Load += new System.EventHandler(this.Dialog_Load);