I have a RichTextBox full of text from a text file I want now to color in red all the places the word "Error" is in the text how to do it?
I tried to do it in the backgroundworker_ProgressChanged
event but its coloring in red some areas in the text and I didn't see in those areas the word "Error".
What am I doing wrong ?
This the part of the code where I'm trying to color the word "Error" in any place in the text. What I want is to paint each place in the text that there is a word "Error" and only the word "Error" and after I want to paint the whole line where the word "Error" is exist in it.
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
this.richTextBox1.AppendText(e.UserState.ToString());
if (e.UserState.ToString().Contains("Error"))
{
Invoke(new Action(() => richTextBox1.SelectionColor = Color.Red));
}
}
And this is the complete code of the new form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class textBoxLoggerViewer : Form
{
//all the text-lines
string[] allText;
开发者_StackOverflow社区 //counter
int lineCounter = 0;
//amount of lines to display
private int maxDisplayAmount = 2000;
string log_file_name = @"\logger.txt";
string logger_file_to_read = Path.GetDirectoryName(Application.LocalUserAppDataPath) + @"\log";
//string logger_file_to_read = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
BackgroundWorker bgw = null;
public textBoxLoggerViewer()
{
InitializeComponent();
label1.Text = "Loading text please wait";
richTextBox1.Font = new Font("Consolas", 8f, FontStyle.Bold);
richTextBox1.BackColor = Color.AliceBlue;
richTextBox1.SelectionColor = Color.Black;
richTextBox1.DoubleClick += new EventHandler(richTextBox1_DoubleClick);
richTextBox1.Enabled = false;
this.Shown += new EventHandler(textBoxLoggerViewer_Shown);
bgw = new BackgroundWorker();
bgw.WorkerReportsProgress = true;
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
this.FormClosed += new FormClosedEventHandler(textBoxLoggerViewer_FormClosed);
}
void textBoxLoggerViewer_FormClosed(object sender, FormClosedEventArgs e)
{
this.bgw.Dispose();
}
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
this.richTextBox1.AppendText(e.UserState.ToString());
if (e.UserState.ToString().Contains("Error"))
{
Invoke(new Action(() => richTextBox1.SelectionColor = Color.Red));
}
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
this.allText = File.ReadAllLines(logger_file_to_read + log_file_name);
while (lineCounter < allText.Length - 1)
{
string current = ReadText();
//get current amount in percent
bgw.ReportProgress((int)(((double)this.lineCounter / (double)allText.Length) * 100), current);
}
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.richTextBox1.Enabled = true;
progressBar1.Enabled = false;
progressBar1.Visible = false;
label1.Text = "All the text have been loaded successfully";
}
void textBoxLoggerViewer_Shown(object sender, EventArgs e)
{
bgw.RunWorkerAsync();
}
void richTextBox1_DoubleClick(object sender, EventArgs e)
{
ReadText();
}
//will be called from bgw
private string ReadText()
{
StringBuilder sb = new StringBuilder();
int curLine = lineCounter;
for (int i = curLine; i < Math.Min(curLine + maxDisplayAmount, allText.Length); i++)
{
sb.Append(i.ToString() + "\t\t" + allText[i] + "\r\n");
lineCounter++;
/* if (allText[i].Contains("Error"))
{
Invoke(new Action(() => richTextBox1.SelectionColor = Color.Red));
}*/
}
return sb.ToString();
}
}
}
BTW: I wanted to add to the progressbar "%" with numbers so when the progressbar is in progress it will count percentages and show them now its just showing this green bar I want in the front of the green bar to show the percentages. like %1 %2 %3 or 1% 2% 3% or without the "%" I don't know how to do it tried many ways didn't work.
Thanks.
You need to select the word Error in the string before you set the selection color:
RichTextBox1.SelectionStart = [redStringStart]
RichTextBox1.SelectionLength = [redStringLength]
RichTextBox1.SelectionColor = Color.Red
精彩评论