Richtextbox中输入关键字就变为蓝色,怎么实现?

猿敲月下码 2009-01-20 11:37:03
如题,就像我们在敲代码的时候,碰到public,class等关键字的时候,这些单词就会变蓝色

请问这个功能在Richtextbox中怎么实现?谢谢了
...全文
233 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
HDNGO 2009-01-20
  • 打赏
  • 举报
回复
    public partial class RichTextBox : Form
{
public RichTextBox()
{
InitializeComponent();
}

private void tSql_TextChanged(object sender, EventArgs e) //文本框改变事件
{
int index = this.tSql.SelectionStart; //记录修改的位置
this.tSql.SelectAll();
this.tSql.SelectionColor = Color.Black;


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);

this.tSql.Select(index, 0); //返回修改的位置
this.tSql.SelectionColor = Color.Black;

}
public int getbunch(string p, string s) //给关键字上色
{
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++)
{
int j;
for (j = 0; j < M; j++)
{
if (ss[i + j] != pp[j]) break;
}
if (j == p.Length)
{
this.tSql.Select(i, p.Length);
this.tSql.SelectionColor = Color.Blue;
cnt++;
}
}
return cnt;

}

}
chinaicm 2009-01-20
  • 打赏
  • 举报
回复
这里有个给关键字着色的Demo
http://tech.ddvip.com/2008-11/122584560888971.html
猿敲月下码 2009-01-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gisfarmer 的回复:]
闪是肯定的了。因为他要适时扫描你的输入啊。除非采用触发式的方式。
[/Quote]
触发式,大侠能指点一二吗?
CutBug 2009-01-20
  • 打赏
  • 举报
回复
mark
优途科技 2009-01-20
  • 打赏
  • 举报
回复
闪是肯定的了。因为他要适时扫描你的输入啊。除非采用触发式的方式。
猿敲月下码 2009-01-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 HDNGO 的回复:]
这个方法会闪的。。。
[/Quote]
下面说的是什么意思,能否解释下
【绘制颜色提议】

  最好的做法是继承RichTextBox,重载新类的Paint方法。

  并且在设置SelectionLength的时候,禁止控件的重绘过程,这样才不会出现被语法高亮的文本有一个突然选中的过程。

  以下2个方法将会对你解决这一问题有很大的帮助.

[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
private const int WM_SETREDRAW = 0xB;//停止控件的重绘
private void BeginPaint()
{
SendMessage(yourRichTextBox.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
}
//允许控件重绘.
private void EndPaint()
{
SendMessage(yourRichTextBox.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
yourRichTextBox.Refresh();
}
HDNGO 2009-01-20
  • 打赏
  • 举报
回复
这个方法会闪的。。。
猿敲月下码 2009-01-20
  • 打赏
  • 举报
回复
试过了 感觉一闪一闪的
猿敲月下码 2009-01-20
  • 打赏
  • 举报
回复
不知道楼上的方法有没光标扫描的现象,我先去试试,如果大家还有什么好的方法也可以说出来,感激不尽
HDNGO 2009-01-20
  • 打赏
  • 举报
回复
    public partial class RichTextBox : Form
{
public RichTextBox()
{
InitializeComponent();
}

private void tSql_TextChanged(object sender, EventArgs e) //文本框改变事件
{
int index = this.tSql.SelectionStart; //记录修改的位置
this.tSql.SelectAll();
this.tSql.SelectionColor = Color.Black;

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);

this.tSql.Select(index, 0); //返回修改的位置
this.tSql.SelectionColor = Color.Black;

}
public int getbunch(string p, string s) //给关键字上色
{
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++)
{
int j;
for (j = 0; j < M; j++)
{
if (ss[i + j] != pp[j]) break;
}
if (j == p.Length)
{
this.tSql.Select(i, p.Length);
this.tSql.SelectionColor = Color.Blue;
cnt++;
}
}
return cnt;
}
}

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧