How can i highlight words in text color the words in yellow?

rhodanny 166 Reputation points
2021-12-30T08:44:34.177+00:00

The highlight method :

void HighlightPhrase(RichTextBox box, string phrase, Color color)
        {
            int pos = box.SelectionStart;
            string s = box.Text;
            for (int ix = 0; ix < s.Length; ix++)
            {
                int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
                if (jx < 0)
                {
                    break;
                }
                else
                {
                    box.SelectionStart = jx;
                    box.SelectionLength = phrase.Length;
                    box.SelectionColor = color;
                    ix = jx + 1;
                    results.Add(jx);
                }
            }
            box.SelectionStart = pos;
            box.SelectionLength = 0;
        }

Then I have a list view selected index changed event :

void lvnf_SelectedIndexChanged(object sender, EventArgs e)
        {
            richTextBox1.Clear();

            if (listViewCostumControl1.lvnf.SelectedItems.Count > 0)
            {
                richTextBox1.Text = File.ReadAllText(listViewCostumControl1.lvnf.Items[listViewCostumControl1.lvnf.SelectedIndices[0]].Text);
                word = textBox1.Text;
                List<string> test = word.Split(new string[] { ",," }, StringSplitOptions.RemoveEmptyEntries).ToList();

                foreach (string myword in test)
                {
                    HighlightPhrase(richTextBox1, myword, Color.Yellow);
                    lblviewerselectedfile.Text = results.Count.ToString();
                    lblviewerselectedfile.Visible = true;
                    if (results.Count > 0)
                    {
                        numericUpDown1.Maximum = results.Count;
                        numericUpDown1.Enabled = true;
                        richTextBox1.SelectionStart = results[(int)numericUpDown1.Value];
                        richTextBox1.ScrollToCaret();
                    }
                }
            }
        }

There are some problems.

The results List is never clear so each time I select another item in the list view it's adding the new results to the List so the results are first 8 then 16 then 34 then 60. I'm not sure where to make a new instance for the results List. I tried in the SelectedIndexChanged event but if I put it before the foreach the results all the time 0 and if I put it in the foreach then it's messing the results. The lblviewerselectedfile that I update with the results not showing the real results.

The second problem is when it's highlighting the words first time it's coloring the first selected item in the list view the words but if the results are 8 it's coloring only 4 words not 8 and then when selecting the second item it's not coloring again anything at all then I select the first item again or other items and it's not coloring and highlighting anything.

I'm trying to highlight with yellow color the words in the richTextBox1 when selecting each time another item in the listView.

I will add some screenshots later.

Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.