using System; using System.Drawing; using System.Windows.Forms; using System.Text.RegularExpressions; //必须引用这一条
namespace ZiFuChuan { /// <summary> /// Description of MainForm. /// </summary> public class MainForm : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; public MainForm() { // // The InitializeComponent() call is required for Windows Forms designer support. // InitializeComponent();
// // TODO: Add constructor code after the InitializeComponent() call. // }
[STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); }
#region Windows Forms Designer generated code /// <summary> /// This method is required for Windows Forms designer support. /// Do not change the method contents inside the source code editor. The Forms designer might /// not be able to load this method if it was changed manually. /// </summary> private void InitializeComponent() { this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox2 // this.textBox2.Location = new System.Drawing.Point(88, 104); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(152, 21); this.textBox2.TabIndex = 1; this.textBox2.Text = "90"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(88, 56); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(272, 21); this.textBox1.TabIndex = 0; this.textBox1.Text = "4,8,12,21,5,65,23,7,128,90,15,87,2,56"; // // button1 // this.button1.Location = new System.Drawing.Point(88, 152); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(88, 24); this.button1.TabIndex = 2; this.button1.Text = "Go"; this.button1.Click += new System.EventHandler(this.Button1Click); // // MainForm // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(432, 237); this.Controls.Add(this.button1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Name = "MainForm"; this.Text = "MainForm"; this.ResumeLayout(false); } #endregion void Button1Click(object sender, System.EventArgs e) { string s = @"[0-9]*[0-9]"; //正则表达式。表示以任意符号分割开的数字字符串 int i=0; bool b = false; Regex r = new Regex(s, RegexOptions.None) ;
foreach( Match m in r.Matches(textBox1.Text)) //与给定的字符串匹配 { s=m.ToString(); i++; if (s==textBox2.Text) //如果相同 { MessageBox.Show (textBox2.Text+ " is the No."+i.ToString() +" number."); b=true; break; } } if (b==false) MessageBox.Show("not found."); }