在RichTextBox中对关键字进行高亮显示

实际效果如下:

相关代码如下:
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Text.RegularExpressions;
None.gif
None.gif
namespace  HighLight
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class Form1 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void HighLightText()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] keywords =   dot.gif{"select","distinct","from","where","order","by","group","null"};
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] functions =   dot.gif"isnull""count""sum" };
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] strings =   dot.gif@"'((.|\n)*?)'" };
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] whiteSpace =   dot.gif"\t""\n""   " };
InBlock.gif
InBlock.gif            rtbSql.SelectAll();
InBlock.gif            rtbSql.SelectionColor 
= Color.Black;
InBlock.gif
InBlock.gif            HighLightText(keywords, Color.Blue);
InBlock.gif            HighLightText(functions, Color.Magenta);
InBlock.gif            HighLightText(strings, Color.Red);
InBlock.gif            HighLightText(whiteSpace, Color.Black);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void HighLightText(string[] wordList, Color color)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (string word in wordList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Regex r 
= new Regex(word, RegexOptions.IgnoreCase);
InBlock.gif
InBlock.gif                
foreach (Match m in r.Matches(rtbSql.Text))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    rtbSql.Select(m.Index, m.Length);
InBlock.gif                    rtbSql.SelectionColor 
= color;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
   
InBlock.gif
InBlock.gif        
private void btnHighLight_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HighLightText();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
若要实现更复杂的功能,可以研究一下这个C#的IDE编辑器的代码。 http://www.icsharpcode.net/OpenSource/SD/Default.aspx
完整源码下载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值