using System;
using System.Collections.Generic;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace IpBox
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Text.RegularExpressions;
![](/Images/OutliningIndicators/InBlock.gif)
public class IpBox : System.Windows.Forms.UserControl
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private string _text = "";
private System.ComponentModel.Container components = null;
![](/Images/OutliningIndicators/InBlock.gif)
public IpBox()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
![](/Images/OutliningIndicators/InBlock.gif)
// TODO: 在 InitializeComponent 调用后添加任何初始化
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
protected override void Dispose(bool disposing)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (disposing)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
组件设计器生成的代码#region 组件设计器生成的代码
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.panel1 = new System.Windows.Forms.Panel();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.White;
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel1.Controls.Add(this.textBox4);
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(this.textBox3);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.textBox2);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.textBox1);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(136, 20);
this.panel1.TabIndex = 1;
//
// textBox4
//
this.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox4.Location = new System.Drawing.Point(104, 0);
this.textBox4.MaxLength = 3;
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(20, 14);
this.textBox4.TabIndex = 6;
this.textBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.textBox4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox4_KeyPress);
//
// label3
//
this.label3.Location = new System.Drawing.Point(96, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(8, 14);
this.label3.TabIndex = 5;
this.label3.Text = ".";
this.label3.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// textBox3
//
this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox3.Location = new System.Drawing.Point(72, 0);
this.textBox3.MaxLength = 3;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(20, 14);
this.textBox3.TabIndex = 4;
this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.textBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox3_KeyPress);
//
// label2
//
this.label2.Location = new System.Drawing.Point(64, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(8, 14);
this.label2.TabIndex = 3;
this.label2.Text = ".";
this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// textBox2
//
this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox2.Location = new System.Drawing.Point(40, 0);
this.textBox2.MaxLength = 3;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(20, 14);
this.textBox2.TabIndex = 2;
this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(8, 14);
this.label1.TabIndex = 1;
this.label1.Text = ".";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// textBox1
//
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox1.Location = new System.Drawing.Point(8, 0);
this.textBox1.MaxLength = 3;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(20, 14);
this.textBox1.TabIndex = 0;
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
//
// IpBox
//
this.Controls.Add(this.panel1);
this.Name = "IpBox";
this.Size = new System.Drawing.Size(136, 20);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//System.Text.Encoding.GetEncoding(0).GetString();
}
![](/Images/OutliningIndicators/InBlock.gif)
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MaskIpAddr(textBox1, e);
}
![](/Images/OutliningIndicators/InBlock.gif)
private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MaskIpAddr(textBox2, e);
}
![](/Images/OutliningIndicators/InBlock.gif)
private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MaskIpAddr(textBox3, e);
}
![](/Images/OutliningIndicators/InBlock.gif)
private void textBox4_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MaskIpAddr(textBox4, e);
}
![](/Images/OutliningIndicators/InBlock.gif)
private void MaskIpAddr(System.Windows.Forms.TextBox textBox, KeyPressEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int len = textBox.Text.Length;
![](/Images/OutliningIndicators/InBlock.gif)
if (Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == 8)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (e.KeyChar != 8)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (len == 2 && e.KeyChar != '.')
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string tmp = textBox.Text + e.KeyChar;
if (textBox.Name == "textBox1")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (Int32.Parse(tmp) > 223) // 进行验证
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。");
textBox.Text = "223";
textBox.Focus();
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
textBox2.Focus();
textBox2.SelectAll();
![](/Images/OutliningIndicators/InBlock.gif)
}
else if (textBox.Name == "textBox2")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (Int32.Parse(tmp) > 255)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "255";
textBox.Focus();
return;
}
textBox3.Focus();
textBox3.SelectAll();
}
else if (textBox.Name == "textBox3")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (Int32.Parse(tmp) > 255)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "255";
textBox.Focus();
return;
}
textBox4.Focus();
textBox4.SelectAll();
}
else if (textBox.Name == "textBox4")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (Int32.Parse(tmp) > 255)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "255";
textBox.Focus();
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
if (e.KeyChar == '.')
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (textBox.Name == "textBox1" && textBox.Text != "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
textBox2.Focus();
textBox2.SelectAll();
}
if (textBox.Name == "textBox2" && textBox.Text != "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
textBox3.Focus();
textBox3.SelectAll();
}
if (textBox.Name == "textBox3" && textBox.Text != "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
textBox4.Focus();
textBox4.SelectAll();
}
if (textBox.Name == "textBox4" && textBox.Text != "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
}
e.Handled = true;
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (textBox.Name == "textBox1" && textBox.Text == "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
}
if (textBox.Name == "textBox2" && textBox.Text == "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
textBox1.Focus();
textBox1.SelectionStart = textBox1.Text.Length;
}
if (textBox.Name == "textBox3" && textBox.Text == "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
textBox2.Focus();
textBox2.SelectionStart = textBox2.Text.Length;
}
if (textBox.Name == "textBox4" && textBox.Text == "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
textBox3.Focus();
textBox3.SelectionStart = textBox3.Text.Length;
}
e.Handled = false;
}
}
else
e.Handled = true;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
[Browsable(true)]
// 获取 IpBox 的文本。
public new string Text
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (textBox1.Text == ""
|| textBox2.Text == ""
|| textBox3.Text == ""
|| textBox4.Text == "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_text = "";
return _text;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_text = Convert.ToInt32(textBox1.Text).ToString() + "." + Convert.ToInt32(textBox2.Text).ToString() + "." + Convert.ToInt32(textBox3.Text).ToString() + "." + Convert.ToInt32(textBox4.Text).ToString();
return _text;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* set //
{
if(value != null)
{
// 255-255-255-255
//string pattern = @"^([1-9]|\d{2}|1[0-9]{1,2}|2[0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})$";
// 223-255-255-255
string pattern = @"^([1-9]|\d{2}|1[0-9]{1,2}|2[1-2][1-3])\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})$";
![](/Images/OutliningIndicators/InBlock.gif)
if( !Regex.IsMatch(value , pattern) )
{
_text = "";
throw new Exception("IP 地址格式错误!");
}
else
{
string[] ipnum = value.Split('.');
textBox1.Text = ipnum[0];
textBox2.Text = ipnum[1];
textBox3.Text = ipnum[2];
textBox4.Text = ipnum[3];
}
}
![](/Images/OutliningIndicators/InBlock.gif)
_text = value;
![](/Images/OutliningIndicators/InBlock.gif)
}
*/
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
posted on
2007-07-15 22:43
石川
阅读(
1320)
评论()
编辑
收藏
举报