效果==代码的编辑器的样子。
代码:
public
partial
class
RichTextBox : Form
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public RichTextBox()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
InitializeComponent();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void tSql_TextChanged(object sender, EventArgs e) //文本框改变事件
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int index = this.tSql.SelectionStart; //记录修改的位置
this.tSql.SelectAll();
this.tSql.SelectionColor = Color.Black;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
string[] keystr =...{ "select ", "from ", "where ", " and ", " or ", " order ", " by ", " desc ", " when ", " case ",
" then ", " end ", " on ", " in ", " is ", " else ", " left ", " join ", " not ", " null " };
for (int i = 0; i < keystr.Length; i++)
this.getbunch(keystr[i], this.tSql.Text);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
this.tSql.Select(index, 0); //返回修改的位置
this.tSql.SelectionColor = Color.Black;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
public int getbunch(string p, string s) //给关键字上色
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int cnt = 0; int M = p.Length; int N = s.Length;
char[] ss = s.ToCharArray(), pp = p.ToCharArray();
if (M > N) return 0;
for (int i = 0; i < N - M + 1; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int j;
for (j = 0; j < M; j++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (ss[i + j] != pp[j]) break;
}
if (j == p.Length)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.tSql.Select(i, p.Length);
this.tSql.SelectionColor = Color.Blue;
cnt++;
}
}
return cnt;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
不好的地方就是出现闪烁。
关键的利用
SelectionColor给文本框内选中的地方上色。。。。。
转载: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1496726