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

來源:互聯網
上載者:User
在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尋找出來的值都會重新加亮顯示,此部分需要改進。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.