2

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;
        }
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Perhaps you have learned that RichTextBox was not the most convenient choice of control to display a table of information. Perhaps a DataGrid or a ListView would be better, permitting you to format a particular cell of your data table more easily. – Wyck Dec 09 '22 at 15:19
  • Does this answer your question? [Highlight all searched words](https://stackoverflow.com/questions/11851908/highlight-all-searched-words) – NineBerry Dec 10 '22 at 02:39
  • The vast majority of displayed information is contained in just 1 line. While I agree that DataGrid would be better option, its just a Vacuum Tube Cross program, it is only used once in a while. – Ijustgiveup Dec 19 '22 at 14:33

2 Answers2

2

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

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
2

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
If you prefer a partial selection, remove both the \b boundaries in the pattern

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;
        };
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
  • In trying more Searches on StackOverflow, this is the best code I have found so far to resolve the issue. Again, thank you very much. Outstanding! Being able to colorize the text makes it easier for the users who are using the Tube Cross Reference program. – Ijustgiveup Dec 12 '22 at 21:03