静态高亮度显示的代码在后面,
如果要功能更复杂,可以研究一下这个C#的IDE编辑器的代码。http://www.icsharpcode.net/OpenSource/SD/Default.aspxusing System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text.RegularExpressions;namespace TheAres.SqlHighLightText
{
/// <summary>
/// Form4 摘要说明。
/// </summary>
public class Form4 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox sqlRichTextBox;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form4()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlRichTextBox = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// sqlRichTextBox
//
this.sqlRichTextBox.Location = new System.Drawing.Point(56, 32);
this.sqlRichTextBox.Name = "sqlRichTextBox";
this.sqlRichTextBox.Size = new System.Drawing.Size(304, 208);
this.sqlRichTextBox.TabIndex = 0;
this.sqlRichTextBox.Text = "select * from table1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(408, 64);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form4
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 294);
this.Controls.Add(this.button1);
this.Controls.Add(this.sqlRichTextBox);
this.Name = "Form4";
this.Text = "Form4";
this.ResumeLayout(false); }
#endregion public static void Main()
{
Application.Run( new Form4() );
} private void HighLightText()
{
string[] keywords = {"select","distinct","from",
"where","order","by","group",
"sum"," is ","null","isnull"};
string[] functions = {"isnull","count","sum"};
string[] strings = {@"'((.|\n)*?)'"};
string[] whiteSpace = {"\t","\n"," "}; this.sqlRichTextBox.SelectAll();
this.sqlRichTextBox.SelectionColor = Color.Black; this.HighLightText(keywords,Color.Blue);
this.HighLightText(functions,Color.Magenta);
this.HighLightText(strings,Color.Red);
this.HighLightText(whiteSpace, Color.Black);
} private void button1_Click(object sender, System.EventArgs e)
{
HighLightText();
}
private void HighLightText(string[] wordList, Color color)
{
foreach (string word in wordList)
{
Regex r = new Regex(word,RegexOptions.IgnoreCase);
foreach(Match m in r.Matches( this.sqlRichTextBox.Text ) )
{
this.sqlRichTextBox.Select(m.Index,m.Length);
this.sqlRichTextBox.SelectionColor = color;
}
}
}
}
}
如果要功能更复杂,可以研究一下这个C#的IDE编辑器的代码。http://www.icsharpcode.net/OpenSource/SD/Default.aspxusing System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text.RegularExpressions;namespace TheAres.SqlHighLightText
{
/// <summary>
/// Form4 摘要说明。
/// </summary>
public class Form4 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox sqlRichTextBox;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form4()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlRichTextBox = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// sqlRichTextBox
//
this.sqlRichTextBox.Location = new System.Drawing.Point(56, 32);
this.sqlRichTextBox.Name = "sqlRichTextBox";
this.sqlRichTextBox.Size = new System.Drawing.Size(304, 208);
this.sqlRichTextBox.TabIndex = 0;
this.sqlRichTextBox.Text = "select * from table1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(408, 64);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form4
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(528, 294);
this.Controls.Add(this.button1);
this.Controls.Add(this.sqlRichTextBox);
this.Name = "Form4";
this.Text = "Form4";
this.ResumeLayout(false); }
#endregion public static void Main()
{
Application.Run( new Form4() );
} private void HighLightText()
{
string[] keywords = {"select","distinct","from",
"where","order","by","group",
"sum"," is ","null","isnull"};
string[] functions = {"isnull","count","sum"};
string[] strings = {@"'((.|\n)*?)'"};
string[] whiteSpace = {"\t","\n"," "}; this.sqlRichTextBox.SelectAll();
this.sqlRichTextBox.SelectionColor = Color.Black; this.HighLightText(keywords,Color.Blue);
this.HighLightText(functions,Color.Magenta);
this.HighLightText(strings,Color.Red);
this.HighLightText(whiteSpace, Color.Black);
} private void button1_Click(object sender, System.EventArgs e)
{
HighLightText();
}
private void HighLightText(string[] wordList, Color color)
{
foreach (string word in wordList)
{
Regex r = new Regex(word,RegexOptions.IgnoreCase);
foreach(Match m in r.Matches( this.sqlRichTextBox.Text ) )
{
this.sqlRichTextBox.Select(m.Index,m.Length);
this.sqlRichTextBox.SelectionColor = color;
}
}
}
}
}
不过实际上我这一次的提问更想得到的是高效的逻辑
我在richtextbox里做Color Syntax的时候,是这样的逻辑
把text按照行分开(Regex.Split(text,"\n"))
|
循环判断每一行是否是注释(比如//)
|
是则取出//前面的字符串(将包括//后面的字符串及其index纪录进一个ArrayList),否则取出整个的字符串
|
然后用正则表达式将取出的字符串分成单个单词与KeyWord字符串数组进行匹配
|
匹配成功则纪录进另一个ArrayList中
|
最后是遍历这两个ArrayList,用RichTextBox的Select来改变颜色我之所以用两个ArrayList,是因为暴露给外面的KeyColor和CommentColor两个属性,可以在
RunTime下选择Color来改变高亮的颜色,当我改变颜色的时候,前面一些匹配的工作就可以不用作了。
可是我发现,最后的一步和ArrayList相关的动作,却花了将近30秒(text大致有1000行)
这是代码:
HighlightWord[] highlighword = (HighlightWord[])arrInput.ToArray(typeof(HighlightWord));
foreach(HighlightWord obj in highlighword)
{
this.Select(obj.index,obj.length);
}
我希望能找到好的逻辑来解决。
3秒
还是太慢
史上最简单最Super的解决办法只有我才有哦!
而且不会flicker闪烁哦!
而且跟sharpdeveloper一样的效果哦!!
想要吗??
真地想要吗?
我要一百五十分~~
我很少来的~~来了就多拿分吧~
大家知道Windows消息吗?
有什么问题MSN联系
我的MSN账号:[email protected]
可不是雅虎通哦!!!
记住了!!
赫赫~~~~
随时在线