c# RichTextBox 使关键字变色

效果==代码的编辑器的样子。

代码:
  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 = 0int 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;

        }


    }


不好的地方就是出现闪烁。
关键的利用 SelectionColor给文本框内选中的地方上色。。。。。

转载: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1496726

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值