question

sharonglipman-9594 avatar image
0 Votes"
sharonglipman-9594 asked JackJJun-MSFT answered

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

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;
         }



dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JackJJun-MSFT avatar image
1 Vote"
JackJJun-MSFT answered

@sharonglipman-9594, 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.



111.gif (127.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.