我尝试在 backgroundworker_ProgressChanged
事件中执行此操作,但它在文本中的某些区域显示为红色,我在这些区域中没有看到“错误”一词。
我做错了什么?
在这部分代码中,我试图在文本的任何位置为单词“Error”着色。我想要的是绘制文本中每个有单词“Error”且只有单词“Error”的地方,然后我想绘制整行存在单词“Error”的地方。
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));
}
}
这是新表单的完整代码:
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;
//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();
}
}
}
顺便说一句:我想用数字添加到进度条“%”,这样当进度条进行时它会计算百分比并显示它们现在它只显示这个绿色条我想在绿色条的前面显示百分比。像 %1 %2 %3 或 1% 2% 3% 或没有“%” 我不知道该怎么做尝试了很多方法都没有用。
谢谢。
最佳答案
在设置选择颜色之前需要先选中字符串中的单词Error:
RichTextBox1.SelectionStart = [redStringStart]
RichTextBox1.SelectionLength = [redStringLength]
RichTextBox1.SelectionColor = Color.Red
关于c# - 我有一个 RichTextBox,里面装满了文本文件中的文本,我现在想将文本中包含单词 "Error"的所有地方都涂成红色,该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7135074/