C# richTextBox中部分關鍵字加亮顯示

在RichTextBox中部分關鍵字加亮顯示的方法: 
使用TextChange事件,循環查找RichTextBox中符合條件的值,加亮顯示。
(如果只加亮顯示一次,只是IndexOf查找,如果有重復出現,且都有加亮顯示,則用LastIndexof查找。)

請看原代碼。

        // 使用TextChange事件,進行加亮顯示(rtbCommand為RichTextBox控件)
        rtbCommand.TextChanged  +=   new  EventHandler(rtbCommand_TextChanged);
        
        
        
        
///   <summary>
        
///  加亮顯示
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>
         void  rtbCommand_TextChanged( object  sender, EventArgs e)
        {
            
            Font fBeforeFont 
=  rtbCommand.Font;
            Color cBeforeColor 
=  rtbCommand.ForeColor;

            
// 定義命的關鍵字
            List < string >  lsCommand  =   new  List < string > ();
            lsCommand.Add(
" ping " );
            lsCommand.Add(
" ipconfig " );
            lsCommand.Add(
" netstat " );
            lsCommand.Add(
" nbtstat " );
            lsCommand.Add(
" ftp " );
            lsCommand.Add(
" tracert " );
            lsCommand.Add(
" pathping " );
            lsCommand.Add(
" nslookup " );
            lsCommand.Add(
" arp " );
            lsCommand.Add(
" net " );
            lsCommand.Add(
" netsh " );
            lsCommand.Add(
" interface " );


            
// 定義參數啟始關鍵字
            List < string >  lsCmdParameter  =   new  List < string > ();
            lsCmdParameter.Add(
" / " );
            lsCmdParameter.Add(
" - " );

            
// 定義啟始索為-1,表示沒有找到
             int  iComPraIndex  =   - 1 ;

            
// 循環所有參數的關鍵,看RichTextBox中是否含有關鍵字
             for  ( int  ICmdPra  =   0 ; ICmdPra  <  lsCmdParameter.Count; ICmdPra ++ )
            {
                
// 索引為最后一個找的字符
                iComPraIndex  =  rtbCommand.Text.ToLower().LastIndexOf(lsCmdParameter[ICmdPra]);
                
if  (iComPraIndex >= 0 )
                {
                    
break ;
                }                
            }
            
// 如果是,就一定不是命令;否則就要判斷是不是命令關鍵字
             if  (iComPraIndex  >=   0 )
            {
                
// 如果找到,剛后兩位加粗,並變色。
                rtbCommand.Select(iComPraIndex,  2 );
                rtbCommand.SelectionColor 
=  Color.Green;
                rtbCommand.SelectionFont 
=   new  Font(rtbCommand.Font, rtbCommand.Font.Style  |  FontStyle.Bold);

                
// 返回成原樣
                rtbCommand.Select(rtbCommand.Text.Length,  0 );
                rtbCommand.SelectionFont 
=  fBeforeFont;
                rtbCommand.SelectionColor 
=  cBeforeColor;
            }
            
else
            {
                
int  iIndex  =   - 1 ;
                
int  iSelectLenght  =   0 ;
                
string  sCommmand  =  rtbCommand.Text;

                
// 循環所有命令的關鍵,看RichTextBox中是否含有關鍵字
                 for  ( int  ilist  =   0 ; ilist  <  lsCommand.Count; ilist ++ )
                {
                    iIndex 
=  rtbCommand.Text.ToLower().IndexOf(lsCommand[ilist]);
                    
if  (iIndex  >=   0 )
                    {
                        
// 得到關鍵字的長度
                        iSelectLenght  =  lsCommand[ilist].Length;

                        
/// /如果找到,命令加粗,並變色。
                        rtbCommand.Select(iIndex, iSelectLenght);
                        rtbCommand.SelectionColor 
=  Color.Blue;
                        rtbCommand.SelectionFont 
=   new  Font(rtbCommand.Font, rtbCommand.Font.Style  |  FontStyle.Bold);
                        
                        
// 返回成原樣
                        rtbCommand.Select(rtbCommand.Text.Length,  0 );
                        rtbCommand.SelectionFont 
=  fBeforeFont;
                        rtbCommand.SelectionColor 
=  cBeforeColor;
                        
break ;
                    }
                }

            }

        }

注:上邊代碼中。使用Indexof查找時,每輸入一個字符,IndexOf查找出來的值都會重新加亮顯示,此部分需要改進。

你可能感兴趣的:(text)