1 静态高亮度显示的代码在后面,   
  2   如果要功能更复杂,可以研究一下这个C#的IDE编辑器的代码。   
  3     
  4   http://www.icsharpcode.net/OpenSource/SD/Default.aspx   
  5     
  6     
  7     
  8   using   System;   
  9   using   System.Drawing;   
 10   using   System.Collections;   
 11   using   System.ComponentModel;   
 12   using   System.Windows.Forms;   
 13   using   System.Text.RegularExpressions;   
 14     
 15   namespace   TheAres.SqlHighLightText   
 16   {   
 17   ///   <summary>   
 18   ///   Form4   摘要说明。   
 19   ///   </summary>   
 20   public   class   Form4   :   System.Windows.Forms.Form   
 21   {   
 22   private   System.Windows.Forms.RichTextBox   sqlRichTextBox;   
 23   private   System.Windows.Forms.Button   button1;   
 24   ///   <summary>   
 25   ///   必需的设计器变量。   
 26   ///   </summary>   
 27   private   System.ComponentModel.Container   components   =   null;   
 28     
 29   public   Form4()   
 30   {   
 31   //   
 32   //   Windows   窗体设计器支持所必需的   
 33   //   
 34   InitializeComponent();   
 35     
 36   //   
 37   //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码   
 38   //   
 39   }   
 40     
 41   ///   <summary>   
 42   ///   清理所有正在使用的资源。   
 43   ///   </summary>   
 44   protected   override   void   Dispose(   bool   disposing   )   
 45   {   
 46   if(   disposing   )   
 47   {   
 48   if(components   !=   null)   
 49   {   
 50   components.Dispose();   
 51   }   
 52   }   
 53   base.Dispose(   disposing   );   
 54   }   
 55     
 56   #region   Windows   窗体设计器生成的代码   
 57   ///   <summary>   
 58   ///   设计器支持所需的方法   -   不要使用代码编辑器修改   
 59   ///   此方法的内容。   
 60   ///   </summary>   
 61   private   void   InitializeComponent()   
 62   {   
 63   this.sqlRichTextBox   =   new   System.Windows.Forms.RichTextBox();   
 64   this.button1   =   new   System.Windows.Forms.Button();   
 65   this.SuspendLayout();   
 66   //     
 67   //   sqlRichTextBox   
 68   //     
 69   this.sqlRichTextBox.Location   =   new   System.Drawing.Point(56,   32);   
 70   this.sqlRichTextBox.Name   =   "sqlRichTextBox";   
 71   this.sqlRichTextBox.Size   =   new   System.Drawing.Size(304,   208);   
 72   this.sqlRichTextBox.TabIndex   =   0;   
 73   this.sqlRichTextBox.Text   =   "select   *   from   table1";   
 74   //     
 75   //   button1   
 76   //     
 77   this.button1.Location   =   new   System.Drawing.Point(408,   64);   
 78   this.button1.Name   =   "button1";   
 79   this.button1.TabIndex   =   1;   
 80   this.button1.Text   =   "button1";   
 81   this.button1.Click   +=   new   System.EventHandler(this.button1_Click);   
 82   //     
 83   //   Form4   
 84   //     
 85   this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);   
 86   this.ClientSize   =   new   System.Drawing.Size(528,   294);   
 87   this.Controls.Add(this.button1);   
 88   this.Controls.Add(this.sqlRichTextBox);   
 89   this.Name   =   "Form4";   
 90   this.Text   =   "Form4";   
 91   this.ResumeLayout(false);   
 92     
 93   }   
 94   #endregion   
 95     
 96   public   static   void   Main()   
 97   {   
 98   Application.Run(   new   Form4()   );   
 99   }   
100     
101   private   void   HighLightText()   
102   {   
103   string[]   keywords   =   {"select","distinct","from",   
104   "where","order","by","group",   
105   "sum","   is   ","null","isnull"};   
106                                                       
107   string[]   functions   =   {"isnull","count","sum"};   
108   string[]   strings   =   {@"'((.|\n)*?)'"};   
109   string[]   whiteSpace   =   {"\t","\n","   "};   
110     
111   this.sqlRichTextBox.SelectAll();   
112   this.sqlRichTextBox.SelectionColor   =   Color.Black;   
113     
114   this.HighLightText(keywords,Color.Blue);   
115   this.HighLightText(functions,Color.Magenta);   
116   this.HighLightText(strings,Color.Red);   
117   this.HighLightText(whiteSpace,   Color.Black);   
118   }   
119     
120   private   void   button1_Click(object   sender,   System.EventArgs   e)   
121   {   
122   HighLightText();   
123     
124   }   
125     
126     
127   private   void   HighLightText(string[]   wordList,   Color   color)   
128   {   
129   foreach   (string   word   in   wordList)   
130   {   
131   Regex   r   =   new   Regex(word,RegexOptions.IgnoreCase);   
132                           
133   foreach(Match   m   in   r.Matches(   this.sqlRichTextBox.Text   )   )   
134   {   
135   this.sqlRichTextBox.Select(m.Index,m.Length);   
136   this.sqlRichTextBox.SelectionColor   =   color;   
137   }   
138   }   
139   }   
140   }   
141   }   
142 
posted on 2006-08-24 16:23  Caviare  阅读(187)  评论(0编辑  收藏  举报