Form1.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:8k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Text.RegularExpressions;
  8. namespace RegexT
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.GroupBox groupBox1;
  16. private System.Windows.Forms.TextBox textBox1;
  17. private System.Windows.Forms.TextBox textBox2;
  18. private System.Windows.Forms.TextBox textBox3;
  19. private System.Windows.Forms.Label label1;
  20. private System.Windows.Forms.Label label2;
  21. private System.Windows.Forms.Button button1;
  22. private System.Windows.Forms.Button button2;
  23. private System.Windows.Forms.CheckBox IgnoreCaseChkBox;
  24. private System.Windows.Forms.CheckBox SingleLineChkBox;
  25. private System.Windows.Forms.CheckBox IgnorePatternWhitespaceChkBox;
  26. private System.Windows.Forms.CheckBox RightToLeftChkBox;
  27. private System.Windows.Forms.CheckBox MultiLineChkBox;
  28. /// <summary>
  29. /// 必需的设计器变量。
  30. /// </summary>
  31. private System.ComponentModel.Container components = null;
  32. public Form1()
  33. {
  34. //
  35. // Windows 窗体设计器支持所必需的
  36. //
  37. InitializeComponent();
  38. //
  39. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  40. //
  41. }
  42. /// <summary>
  43. /// 清理所有正在使用的资源。
  44. /// </summary>
  45. protected override void Dispose( bool disposing )
  46. {
  47. if( disposing )
  48. {
  49. if (components != null) 
  50. {
  51. components.Dispose();
  52. }
  53. }
  54. base.Dispose( disposing );
  55. }
  56. private RegexOptions GetSel()
  57. {
  58. RegexOptions sel = RegexOptions.None;
  59. if(this.IgnoreCaseChkBox.Checked)
  60. sel |= RegexOptions.IgnoreCase;
  61. if(this.IgnorePatternWhitespaceChkBox.Checked)
  62. sel |= RegexOptions.IgnorePatternWhitespace;
  63. if(this.MultiLineChkBox.Checked)
  64. sel |= RegexOptions.Multiline;
  65. if(this.SingleLineChkBox.Checked)
  66. sel |= RegexOptions.Singleline;
  67. if(this.RightToLeftChkBox.Checked)
  68. sel |= RegexOptions.RightToLeft;
  69. return sel;
  70. }
  71. #region Windows 窗体设计器生成的代码
  72. /// <summary>
  73. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  74. /// 此方法的内容。
  75. /// </summary>
  76. private void InitializeComponent()
  77. {
  78. this.groupBox1 = new System.Windows.Forms.GroupBox();
  79. this.IgnoreCaseChkBox = new System.Windows.Forms.CheckBox();
  80. this.SingleLineChkBox = new System.Windows.Forms.CheckBox();
  81. this.IgnorePatternWhitespaceChkBox = new System.Windows.Forms.CheckBox();
  82. this.RightToLeftChkBox = new System.Windows.Forms.CheckBox();
  83. this.MultiLineChkBox = new System.Windows.Forms.CheckBox();
  84. this.textBox1 = new System.Windows.Forms.TextBox();
  85. this.textBox2 = new System.Windows.Forms.TextBox();
  86. this.textBox3 = new System.Windows.Forms.TextBox();
  87. this.label1 = new System.Windows.Forms.Label();
  88. this.label2 = new System.Windows.Forms.Label();
  89. this.button1 = new System.Windows.Forms.Button();
  90. this.button2 = new System.Windows.Forms.Button();
  91. this.groupBox1.SuspendLayout();
  92. this.SuspendLayout();
  93. // 
  94. // groupBox1
  95. // 
  96. this.groupBox1.Controls.Add(this.MultiLineChkBox);
  97. this.groupBox1.Controls.Add(this.RightToLeftChkBox);
  98. this.groupBox1.Controls.Add(this.IgnorePatternWhitespaceChkBox);
  99. this.groupBox1.Controls.Add(this.SingleLineChkBox);
  100. this.groupBox1.Controls.Add(this.IgnoreCaseChkBox);
  101. this.groupBox1.Location = new System.Drawing.Point(8, 96);
  102. this.groupBox1.Name = "groupBox1";
  103. this.groupBox1.Size = new System.Drawing.Size(368, 112);
  104. this.groupBox1.TabIndex = 0;
  105. this.groupBox1.TabStop = false;
  106. this.groupBox1.Text = "选项";
  107. // 
  108. // IgnoreCaseChkBox
  109. // 
  110. this.IgnoreCaseChkBox.Location = new System.Drawing.Point(8, 16);
  111. this.IgnoreCaseChkBox.Name = "IgnoreCaseChkBox";
  112. this.IgnoreCaseChkBox.TabIndex = 0;
  113. this.IgnoreCaseChkBox.Text = "忽略大小写";
  114. // 
  115. // SingleLineChkBox
  116. // 
  117. this.SingleLineChkBox.Location = new System.Drawing.Point(128, 16);
  118. this.SingleLineChkBox.Name = "SingleLineChkBox";
  119. this.SingleLineChkBox.TabIndex = 1;
  120. this.SingleLineChkBox.Text = "单行模式";
  121. // 
  122. // IgnorePatternWhitespaceChkBox
  123. // 
  124. this.IgnorePatternWhitespaceChkBox.Location = new System.Drawing.Point(8, 48);
  125. this.IgnorePatternWhitespaceChkBox.Name = "IgnorePatternWhitespaceChkBox";
  126. this.IgnorePatternWhitespaceChkBox.Size = new System.Drawing.Size(168, 24);
  127. this.IgnorePatternWhitespaceChkBox.TabIndex = 2;
  128. this.IgnorePatternWhitespaceChkBox.Text = "消除模式空白处理";
  129. // 
  130. // RightToLeftChkBox
  131. // 
  132. this.RightToLeftChkBox.Location = new System.Drawing.Point(224, 48);
  133. this.RightToLeftChkBox.Name = "RightToLeftChkBox";
  134. this.RightToLeftChkBox.TabIndex = 3;
  135. this.RightToLeftChkBox.Text = "右至左";
  136. // 
  137. // MultiLineChkBox
  138. // 
  139. this.MultiLineChkBox.Location = new System.Drawing.Point(224, 16);
  140. this.MultiLineChkBox.Name = "MultiLineChkBox";
  141. this.MultiLineChkBox.TabIndex = 4;
  142. this.MultiLineChkBox.Text = "多行模式";
  143. // 
  144. // textBox1
  145. // 
  146. this.textBox1.Location = new System.Drawing.Point(8, 32);
  147. this.textBox1.Multiline = true;
  148. this.textBox1.Name = "textBox1";
  149. this.textBox1.Size = new System.Drawing.Size(368, 56);
  150. this.textBox1.TabIndex = 1;
  151. this.textBox1.Text = "";
  152. // 
  153. // textBox2
  154. // 
  155. this.textBox2.Location = new System.Drawing.Point(8, 248);
  156. this.textBox2.Multiline = true;
  157. this.textBox2.Name = "textBox2";
  158. this.textBox2.Size = new System.Drawing.Size(368, 72);
  159. this.textBox2.TabIndex = 2;
  160. this.textBox2.Text = "";
  161. // 
  162. // textBox3
  163. // 
  164. this.textBox3.Location = new System.Drawing.Point(8, 328);
  165. this.textBox3.Name = "textBox3";
  166. this.textBox3.TabIndex = 3;
  167. this.textBox3.Text = "";
  168. // 
  169. // label1
  170. // 
  171. this.label1.Location = new System.Drawing.Point(8, 8);
  172. this.label1.Name = "label1";
  173. this.label1.Size = new System.Drawing.Size(100, 16);
  174. this.label1.TabIndex = 4;
  175. this.label1.Text = "标准式";
  176. // 
  177. // label2
  178. // 
  179. this.label2.Location = new System.Drawing.Point(8, 216);
  180. this.label2.Name = "label2";
  181. this.label2.TabIndex = 5;
  182. this.label2.Text = "应用检测试";
  183. // 
  184. // button1
  185. // 
  186. this.button1.Location = new System.Drawing.Point(136, 328);
  187. this.button1.Name = "button1";
  188. this.button1.Size = new System.Drawing.Size(112, 23);
  189. this.button1.TabIndex = 6;
  190. this.button1.Text = "验证";
  191. this.button1.Click += new System.EventHandler(this.button1_Click);
  192. // 
  193. // button2
  194. // 
  195. this.button2.Location = new System.Drawing.Point(272, 328);
  196. this.button2.Name = "button2";
  197. this.button2.Size = new System.Drawing.Size(104, 23);
  198. this.button2.TabIndex = 7;
  199. this.button2.Text = "清空";
  200. this.button2.Click += new System.EventHandler(this.button2_Click);
  201. // 
  202. // Form1
  203. // 
  204. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  205. this.ClientSize = new System.Drawing.Size(384, 381);
  206. this.Controls.Add(this.button2);
  207. this.Controls.Add(this.button1);
  208. this.Controls.Add(this.label2);
  209. this.Controls.Add(this.label1);
  210. this.Controls.Add(this.textBox3);
  211. this.Controls.Add(this.textBox2);
  212. this.Controls.Add(this.textBox1);
  213. this.Controls.Add(this.groupBox1);
  214. this.Name = "Form1";
  215. this.Text = "Form1";
  216. this.groupBox1.ResumeLayout(false);
  217. this.ResumeLayout(false);
  218. }
  219. #endregion
  220. /// <summary>
  221. /// 应用程序的主入口点。
  222. /// </summary>
  223. [STAThread]
  224. static void Main() 
  225. {
  226. Application.Run(new Form1());
  227. }
  228. private void button1_Click(object sender, System.EventArgs e)
  229. {
  230. try
  231. {
  232. RegexOptions sel = this.GetSel();
  233. Regex RegexT = new Regex(textBox1.Text,sel);
  234. if(RegexT.IsMatch(textBox2.Text))
  235. {
  236. textBox3.ForeColor = Color.Black;
  237. textBox3.Text = "正则表达式匹配";
  238. }
  239. else
  240. {
  241. textBox3.ForeColor = Color.Red;
  242. textBox3.Text = "正则表达式不匹配";
  243. }
  244. }
  245. catch(ArgumentException ax)
  246. {
  247. textBox3.ForeColor = Color.Red;
  248. textBox3.Text = "出现错误:"+ax.Message.ToString(); 
  249. }
  250. }
  251. private void button2_Click(object sender, System.EventArgs e)
  252. {
  253.      textBox3.Text= "";
  254.              textBox2.Text= "";
  255.  textBox1.Text= "";
  256. }
  257. }
  258. }