Reputation: 45
After browsing a .txt file I stored each line in an array (i.e. filetext)...
String[] filetext = File.ReadAllLines(filepath);
How can i highlight particular lines with a color and show them in RichTextBox?
Upvotes: 2
Views: 929
Reputation: 45
I found solution with the help of above code.
There is no need to store each line in an array.
for (int i = 0; i < ptrsize; i++)
{
richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(linenum[i]),richTextBox1.Lines[linenum[i]].Length);
richTextBox1.SelectionColor = Color.Red;
}
Upvotes: 0
Reputation: 3306
You can do it with a simple method (in your forms code):
private void ShowText(string[] text)
{
richTextBox1.Clear();
richTextBox1.Text = string.Join(Environment.NewLine, text);
}
and given a line number/index:
private void HighlightLine(int lineIdx)
{
richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(lineIdx), richTextBox1.Lines[lineIdx].Length);
richTextBox1.SelectionColor = Color.Red;
}
Upvotes: 1