Why when highlighting word/s in richTextBox it's highlighting all the text in the richTextBox ?

sharon glipman 441 Reputation points
2021-08-26T05:19:23.157+00:00

I have a list of items in a listView control name lvnf.

When I select an item in the listView it's reading the item file on the hard disk and show it's content in a richTextBox1.

After adding the file content to the richTextBox1 I want to highlight specific words in the richTextBox1 but coloring this words in yellow.

The problem is that it's coloring all the content in the richTextBox1.

myword in the foreach loop is each word(string) I want to color in yellow in the richTextBox1 but it's coloring all the content in richTextBox1 in yellow.

void lvnf_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewCostumControl1.lvnf.SelectedItems.Count > 0)
            {
                results = new List<int>();
                richTextBox1.Text = File.ReadAllText(listViewCostumControl1.lvnf.Items[listViewCostumControl1.lvnf.SelectedIndices[0]].Text);
                FileInfo fi = new FileInfo(listViewCostumControl1.lvnf.Items[listViewCostumControl1.lvnf.SelectedIndices[0]].Text);
                lblfilesizeselected.Text = ExtensionMethods.ToFileSize(fi.Length);
                lblfilesizeselected.Visible = true;
                filePath = Path.GetDirectoryName(fi.FullName);
                string word = textBoxSearchBox.Text;
                string[] test = word.Split(new string[] { ",," }, StringSplitOptions.None);
                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];
                    }
                }
            }
        }

This method is the HighlightPhrase that should find the word/s in the richTextBox1

void HighlightPhrase(RichTextBox box, string phrase, Color color)
        {
            int pos = box.SelectionStart;
            string s = box.Text;
            for (int ix = 0; ;)
            {
                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;
        }
Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-08-26T06:46:25.587+00:00

    @sharon glipman , based on my test, I find that I can run your code to color the specific word in the Richtextbox.

    Also, Please note that you missing some code here, I make some changes on it.

    Change this

      for (int ix = 0; ;)  
    

    into:

      for (int ix = 0;ix<s.Length ;ix++)  
    

    Here is a code example that works for me.(Only Changed the above code)

    List<int> results = new List<int>();  
            private void listView1_SelectedIndexChanged(object sender, EventArgs e)  
            {  
               
                if (listView1.SelectedItems.Count > 0)  
                {  
                    richTextBox1.Text = File.ReadAllText(listView1.Items[listView1.SelectedIndices[0]].Text);  
                    FileInfo fi = new FileInfo(listView1.Items[listView1.SelectedIndices[0]].Text);  
                    //lblfilesizeselected.Text = ExtensionMethods.ToFileSize(fi.Length);  
                    //lblfilesizeselected.Visible = true;  
                    string filePath = Path.GetDirectoryName(fi.FullName);  
                    string word = textBox1.Text;  
                    string[] test = word.Split(new string[] { ",," }, StringSplitOptions.None);  
                    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];  
                        }  
                    }  
                }  
            }  
      
            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;  
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                listView1.SelectedIndexChanged += listView1_SelectedIndexChanged;  
                listView1.View = View.Details;  
                listView1.Columns.Add("Path");  
                listView1.Columns.Add("Name");  
                ListViewItem lvi = new ListViewItem();  
                lvi.Text = "D:\\1.txt";  
                lvi.SubItems.Add("test1");  
                ListViewItem lvi1 = new ListViewItem();  
                lvi1.Text = "D:\\2.txt";  
                lvi1.SubItems.Add("test2");  
                ListViewItem lvi2 = new ListViewItem();  
                lvi2.Text = "D:\\3.txt";  
                lvi2.SubItems.Add("test3");  
                listView1.Items.Add(lvi);  
                listView1.Items.Add(lvi1);  
                listView1.Items.Add(lvi2);  
            }  
    

    Result:
    126647-111.gif


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.