hoTools.ActiveX.AddinControlGui.ColorCharacters C# (CSharp) Method

ColorCharacters() public method

Color find the found search string in rtf textbox. The string has to exactly match.
public ColorCharacters ( System.Windows.Forms.RichTextBox rtf, string fromText, string stringToColor, Color color ) : void
rtf System.Windows.Forms.RichTextBox
fromText string
stringToColor string
color Color
return void
        void ColorCharacters(RichTextBox rtf, string fromText, string stringToColor, Color color)
        {
            int posInText = 0;
            if (stringToColor.Trim() == "")
            {
                rtf.SelectionBackColor = Color.AliceBlue;
                rtf.AppendText(fromText);
            }
            else
            {

                string from = fromText;
                Regex pattern = new Regex($"{stringToColor.Trim()}",RegexOptions.IgnoreCase);
                Match match = pattern.Match(from);
                while (match.Success)
                {
                    // output not outputted text
                    if ((match.Index - posInText) > 0)
                    {
                        rtf.SelectionBackColor = Color.AliceBlue;
                        rtf.AppendText(from.Substring(posInText, match.Index - posInText));
                    }
                    rtf.SelectionBackColor = Color.Gold;
                    rtf.AppendText(match.Value);
                    posInText = match.Index + match.Length;
                    match = match.NextMatch();

                }
                if ((from.Length - 1 - posInText) >= 0)
                {
                    rtf.SelectionBackColor = Color.AliceBlue;
                    rtf.SelectionBackColor = Color.AliceBlue;
                    rtf.AppendText(from.Substring(posInText, from.Length - posInText));
                }
        }
    }
AddinControlGui