如题。没事做个练习代码的小程序。在richTextBox中写出代码,如果是关键字则高亮。要求不要用xml,直接在程序里定义关键字。谢谢各位帮忙。小弟没分了。

解决方案 »

  1.   

    怎么没人。
    我想这样定义关键字
    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;
            }
    具体的操作就不会了。 
      

  2.   

    高亮是不是可以通过color来实现呢?
    具体实现俺就不知道了...
      

  3.   

    楼上的。很抱歉你猜错了。只是我自己写的感觉效果不好。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;
            }
      

  4.   

    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 高亮