可以通过自定义控件的方式做一个IP输入框,目前这个是一个IP输入控件的类,还不能在工具箱中拖放,希望以后做一个可以放在工具箱中的,这个类如下:
public class IpInputBox : Label
{
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.BorderStyle = BorderStyle.FixedSingle;
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, 3);
_dotLabel1.Location = new System.Drawing.Point(29, 3);
_box2.Location = new System.Drawing.Point(39, 3);
_dotLabel2.Location = new System.Drawing.Point(69,3);
_box3.Location = new System.Drawing.Point(79,3);
_dotLabel3.Location = new System.Drawing.Point(109,3);
_box4.Location = new System.Drawing.Point(119,3);
_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();
}
public class SubIpInputBox : TextBox
{
/// <summary>
/// 判断是否是子网掩码,false表示ip地址,true表示子网掩码
/// </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;
//定义事件函数
public void FallBackEventFun(int flag)
{
if (TextFallBackEvent != null)
{
TextFallBackEvent(box, this.Flag);
}
}
public bool IsNetmask
{
get { return _isNetmask; }
set { _isNetmask = value; }
}
/// <summary>
/// 构造函数
/// </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, 28);
this.MaxLength = 3;
}
public SubIpInputBox(string str)
{
this.Text = str;
this.Size = new System.Drawing.Size(30, 28);
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("编号最多只能输入3位数字!");
}
}
}
else if (e.KeyChar == '.')
{
//MessageBox.Show("编号只能输入数字!");
if (this.Text.Length != 0)
{
// 如果输入 . 就执行 TAB 键
SendKeys.SendWait("{TAB}");
//MessageBox.Show(this.Name);
}
}
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 > 255 || (currentNumber == 0 && this.Flag == 1))
{
MessageBox.Show("你输入的" + currentStr + "不是有效数值,请指定一个介于1到223之间的数值!", "错误", 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;//设置光标在末尾
this.SelectAll();
}
else
{
if (currentStr.Length == 3 && _isSendKey == false)
{// 当输入的字符个数为3时,跳入另外一个输入框
//MessageBox.Show("输入完毕");
if (currentNumber == 0)
{
this.Text = "";
MessageBox.Show("000");
}
SendKeys.SendWait("{TAB}");
}
}
}
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)
{// 当输入的字符个数为3时,跳入另外一个输入框
//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)
{
}
}
这个控件中继承自Label控件的,在Label中加入4个输入框和,ip分隔的点,就可以了,
目前只是在使用中还不是那么的方便,
在使用的地方
private IpInputBox ipBox1 = new IpInputBox(false);
private IpInputBox ipBox2 = new IpInputBox(false);
然后把这个控件加入到窗体控件中
并设置Location就可以
为了方便控件的定位,可以在窗体上放一个panel,然后在panel中添加这个自定义的ip输入框
this.panel1.Controls.Add(ipBox)