Home > Enterprise >  Iterate through RichTextBox making specific words bold
Iterate through RichTextBox making specific words bold

Time:12-10

I wrote a Vacuum tube Cross reference program. I can't figure out how to iterate through the RichTextBox1 to make the requested tube value Bold.

Existing output when searching for 6AU8A

Tube        Cross1      Cross2      Cross3          Qty     Location
6AU8        6AU8A       6BH8        6AW8            2       PM BOX 3
6AU8A       6AU8        6BH8        6AW8            6       BOX 9
6BA8A       6AU8        6AU8A       8AU8A           1       BOX 11
6CX8        6AU8A       6EB8        6GN8            2       BOX 16
6EH5        6AU8A       2081        6AW8A#          1       BOX 19
6GH8        6EA8        6GH8A       6AU8A           2       BOX 23
6GH8A       6GH8        6EA8        6AU8A           10      BOX 22
6GH8A       6GH8        6EA8        6AU8A           5       BOX 23

So, I need any occurrence for search term (6AU8A in this example) in Bold. Using VS 2019, Windows Application, compiled for .NET 4.8.1, runs on Windows 7 & 10 PC's.

public int FindMyText(string text)
        {
            length = text.Length;
            // Initialize the return value to false by default.
            int returnValue = -1;

            // Ensure that a search string has been specified and a valid start point.
            if (text.Length > 0)
            {
                // Obtain the location of the first character found in the control
                // that matches any of the characters in the char array.
                int indexToText = richTextBox1.Find(text);
                // Determine whether the text was found in richTextBox1.
                if (indexToText >= 0)
                {
                    // Return the location of the character.
                    returnValue = indexToText;
                    start = indexToText;
                }
            }

            return returnValue;
        }

CodePudding user response:

Once you call Find(), the value will be SELECTED (if it exists). Then you can change the Font to include Bold. The Find() function has a "Start" position, which allows you to find the NEXT occurrence. So you start at 0, then add the length of the search string to get the next starting position. If the value returned is -1, then there were no more matches and you can stop.

Looks something like this:

private void button1_Click(object sender, EventArgs e)
{
    BoldText("6AU8A"); // get your input from somewhere...
}

private void BoldText(String value)
{
    int startAt = 0;
    int indexToText = richTextBox1.Find(value, startAt, RichTextBoxFinds.None);
    while (indexToText != -1)
    {
        richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold);
        startAt = startAt   value.Length;
        indexToText = richTextBox1.Find(value, startAt, RichTextBoxFinds.None);
    }
}

Here it is in action:

enter image description here

CodePudding user response:

If you actually need to use a RichTextBox for this, you could use a simple Regex to match one or more terms, then use Index and Length properties of each Match to perform the selection and change the Font and/or other styles (in the code here, optionally, the color)

Pass a collection of terms to match (containing one or more terms) to the method:

string[] parts = { "6AU8A", "6GH8" };
Highlight(richTextBox1, parts, FontStyle.Bold);
// also specifying a Color
Highlight(richTextBox1, parts, FontStyle.Bold, Color.Red);
// or deselect previous matches
Highlight(richTextBox1, parts, FontStyle.Regular);

The Regex only matches complete sequences, e.g., passing 6GH8, it won't partially highlight 6GH8A

using System.Text.RegularExpressions;

private void Highlight(RichTextBox rtb, IEnumerable<string> terms, FontStyle style, Color color = default)
{
    color = color == default ? rtb.ForeColor : color;

    string pattern = $@"\b(?:{string.Join("|", terms)})\b";
    var matches = Regex.Matches(rtb.Text, pattern, RegexOptions.IgnoreCase);

    using (var font = new Font(rtb.Font, style)) { 
        foreach (Match m in matches) {
            rtb.Select(m.Index, m.Length);
            rtb.SelectionColor = color;
            rtb.SelectionFont = font;
        };
    }
}
  • Related