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 :

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