question

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

Reading from a richTextBox is too slow. Is there a way to make it faster ?

The goal is to split words from richTextBox2 and highlight the words in yellow.

Technic it's working but too slow.

So I did a test.

The original line is :

 string word = richTextBox2.Text;

The content of richTextBox2 is the top window in the screenshot the bottom window is the listView where I select items and read the content from the richTextBox :

127331-a1.jpg

When using the line :

  string word = richTextBox2.Text;

Everything is very slow I select items in the listView and it's very slow.

I tested with my own line :

 string word = "using,,using";

And with this line it's working very fast.

I also have to mention that before richTextBox2 I used a simple regular textBox control and it was working fast but I can't highlight chars and strings in textBox so I changed it to richTextBox but than everything is slow.


 void lvnf_SelectedIndexChanged(object sender, EventArgs e)
         {
             List<int> results = new List<int>();
             richTextBox1.Clear();
    
             if (listViewCostumControl1.lvnf.SelectedItems.Count > 0)
             {
                 richTextBox1.Text = File.ReadAllText(listViewCostumControl1.lvnf.Items[listViewCostumControl1.lvnf.SelectedIndices[0]].Text);
                 string word = richTextBox2.Text;
                 //string word = "using,,using";
                 string[] test = word.Split(new string[] { ",," }, StringSplitOptions.None);
                 foreach (string myword in test)
                 {
                     HighlightPhrase(richTextBox1, myword, Color.Yellow);
                     if (results.Count > 0)
                     {
                         numericUpDown1.Maximum = results.Count;
                         numericUpDown1.Enabled = true;
                         richTextBox1.SelectionStart = results[(int)numericUpDown1.Value];
                     }
                 }
             }
         }


And the HighlightPhrase 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;
         }




dotnet-csharpwindows-forms
a1.jpg (165.7 KiB)
· 1
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.

@sharonglipman-9594 , I have some questions about your problem. First, the code string word = richTextBox2.Text; is only to get the text from the richtext2, but I didn't see that you highlight the string in richtextbox2. Based on my test, the code will only highlight the text in richtextbox1.
Based on my test, the code works well without the problem being too slow.

I also have to mention that before richTextBox2 I used a simple regular textBox control and it was working fast but I can't highlight chars and strings in textBox so I changed it to richTextBox but than everything is slow.

Finally, I am not full understand that the reason you changed textbox to richtextbox. The textbox is only used to search the word in the richtextbox1. Do you mean that you want to highlight the text in textbox?


0 Votes 0 ·

1 Answer

periczeljkosmederevo avatar image
0 Votes"
periczeljkosmederevo answered

I would use this aproach to solve this 'light' problem :

Load letter by letter from selected document and add it to rich text box, colored in desired collor. When separators are loaded add them in different collor.


This way solution becomes simply direct copy of document text to rich text box , with highlighted words .

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.