WinForm richtextbox 關鍵字變紅色


        private void HilightRichText(RichTextBox control, string hilightString)
        {
            int nSelectStart = control.SelectionStart;  
            int nSelectLength = control.SelectionLength;
            int nIndex = 0;
            while (nIndex < control.Text.Length)
            {
                nIndex = control.Find(hilightString, nIndex, RichTextBoxFinds.WholeWord); //整個文檔查找
                if (nIndex < 0)
                {
                    break;                     
                }
                control.Select(nIndex, hilightString.Length);      //選擇文本
                control.SelectionColor = Color.Red;
                nIndex += hilightString.Length;
            }
            control.Select(nSelectStart, nSelectLength);
        }

文檔

相關文章
相關標籤/搜索