首页 > 代码库 > WinForm richtextbox 关键字变红色

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);
        }

WinForm richtextbox 关键字变红色