richTextBox高亮关键字 如题。没事做个练习代码的小程序。在richTextBox中写出代码,如果是关键字则高亮。要求不要用xml,直接在程序里定义关键字。谢谢各位帮忙。小弟没分了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 怎么没人。我想这样定义关键字public static List<string> AllClass() { List<string> list = new List<string>(); list.Add( "string" ); list.Add( "return" ); list.Add( "class" ); list.Add( "new" ); list.Add( "extends" ); list.Add( "var" ); return list; }具体的操作就不会了。 高亮是不是可以通过color来实现呢?具体实现俺就不知道了... 楼上的。很抱歉你猜错了。只是我自己写的感觉效果不好。private void rtbMain_TextChanged( object sender, EventArgs e ) { int index = this.rtbMain.SelectionStart; //记录修改的位置 this.rtbMain.SelectAll(); this.rtbMain.SelectionColor = Color.Black; string[] keystr ={ "string ", "int ", "#region ", "#endregion ", "private ", "void ", "this ", "public ", "char ", "return ", "if ", "else ", "for ", "ref ", "new ", "try ", "catch ", "true ", "false ", "not ", "null ", "using ", "namespace ", "partial ", "class ", "protected ", "float ", "foreach ", "in ", "object "}; for (int i = 0; i < keystr.Length; i++) this.getbunch(keystr[i], this.rtbMain.Text); this.rtbMain.Select(index, 0); //返回修改的位置 this.rtbMain.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.rtbMain.Select( i, p.Length ); this.rtbMain.SelectionColor = Color.Blue; cnt++; } } return cnt; } Font f=new Font("Verdana", 8F, FontStyle.Italic, GraphicsUnit.Point);string str="";if (richTextBox1.Find(str)>0){int pos=richTextBox1.Find(str);richTextBox1.SelectionStart=pos;richTextBox1.SelectionLength=str.Length; richTextBox1.SelectionFont=f;richTextBox1.SelectionColor=Color.Red;} richTextBox 高亮 串口通信如何判断超时???[ c#弹出对话框得问题 关于一个label变色的问题 求jquery的跑马灯效果 如何生成系统默认Unicode字符编码编码的txt文件 使用Remoting做过大型工程请进 求助,我是c++程序员,公司让我学习c#,并在.net下做程序,请问我需要多少精力,值不值的? 那位大哥帮帮我改改,急啊!!!!!!!!! 广州的朋友进来看看。。。。 C#将EXCEL里面的数据导入数据库 VSTO开发的word2007插件如何部署到客户端机器上 给大家分享个冒泡排序
我想这样定义关键字
public static List<string> AllClass()
{
List<string> list = new List<string>();
list.Add( "string" );
list.Add( "return" );
list.Add( "class" );
list.Add( "new" );
list.Add( "extends" );
list.Add( "var" );
return list;
}
具体的操作就不会了。
具体实现俺就不知道了...
{
int index = this.rtbMain.SelectionStart; //记录修改的位置
this.rtbMain.SelectAll();
this.rtbMain.SelectionColor = Color.Black;
string[] keystr ={ "string ", "int ", "#region ", "#endregion ", "private ", "void ", "this ", "public ", "char ", "return ",
"if ", "else ", "for ", "ref ", "new ", "try ", "catch ", "true ", "false ", "not ", "null ",
"using ", "namespace ", "partial ", "class ", "protected ", "float ", "foreach ", "in ",
"object "};
for (int i = 0; i < keystr.Length; i++)
this.getbunch(keystr[i], this.rtbMain.Text);
this.rtbMain.Select(index, 0); //返回修改的位置
this.rtbMain.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.rtbMain.Select( i, p.Length );
this.rtbMain.SelectionColor = Color.Blue;
cnt++;
}
}
return cnt;
}
string str="";
if (richTextBox1.Find(str)>0)
{
int pos=richTextBox1.Find(str);
richTextBox1.SelectionStart=pos;
richTextBox1.SelectionLength=str.Length;
richTextBox1.SelectionFont=f;
richTextBox1.SelectionColor=Color.Red;
}
richTextBox 高亮