首先我们先设计好一个界面,如下图:
需要写一个设计器支持所需的代码方法:
public Frm_Main()
{
InitializeComponent();
}
namespace ValidateIP
{
partial class Frm_Main
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.btn_Validate = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(112, 31);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(171, 21);
this.textBox1.TabIndex = 0;
//
// btn_Validate
//
this.btn_Validate.Location = new System.Drawing.Point(86, 20);
this.btn_Validate.Name = "btn_Validate";
this.btn_Validate.Size = new System.Drawing.Size(141, 23);
this.btn_Validate.TabIndex = 1;
this.btn_Validate.Text = "验证IP";
this.btn_Validate.UseVisualStyleBackColor = true;
this.btn_Validate.Click += new System.EventHandler(this.btn_Validate_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(23, 35);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(89, 12);
this.label1.TabIndex = 2;
this.label1.Text = "请输入IP地址:";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Location = new System.Drawing.Point(7, 6);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(315, 76);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "输入信息";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.btn_Validate);
this.groupBox2.Location = new System.Drawing.Point(7, 89);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(315, 54);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "操作";
//
// Frm_Main
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(329, 146);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Frm_Main";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "使用正则表达式验证IP地址";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btn_Validate;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
}
}
接下来双击“验证IP”button 会进入到它的点击事件
private void btn_Validate_Click(object sender, EventArgs e)
{
if (IPCheck(textBox1.Text))//验证IP是否正确
{
MessageBox.Show("输入IP正确");//弹出消息对话框
}
else { MessageBox.Show("输入IP不正确"); }//弹出消息对话框
}
/// <summary>
/// 验证IP是否正确
/// </summary>
/// <param name="IP">IP地址字符串</param>
/// <returns>方法返回布尔值</returns>
public bool IPCheck(string IP)
{
string num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";//创建正则表达式字符串
return Regex.IsMatch(IP,//使用正则表达式判断是否匹配
("^" + num + "\\." + num + "\\." + num + "\\." + num + "$"));
}
运行效果如下: