我有一個 RichTextBox 記錄有關我的應用程式的資訊。以下是它可能記錄的示例:
<22:52:21:179> Starting Argo Studio
<22:52:22:731> Argo Studio has finished starting
<22:52:30:41> Time to load commands: 00:00:00.00
<22:52:30:48> Created 'App 1'
<和之間的文字>是時間。
我需要將時間的顏色更改為灰色。
以前,我是這樣做的:
for (int i = 0; i < RichTextBox.Lines.Length; i )
{
int indexStart = RichTextBox.GetFirstCharIndexFromLine(i);
int indexEnd = RichTextBox.Lines[i].Split(' ')[0].Length;
RichTextBox.Select(indexStart, indexEnd);
RichTextBox.SelectionColor = Color.Gray;
}
但是,這不再適用于我,因為現在我有多行日志:
<23:0:4:320> Error-h88tzd: The source and destination are the same.
Source: 'C:\Users\user\Dropbox\PC\Desktop\...'.
Destination: 'C:\Users\user\Dropbox\PC\Desktop\....
More information: https://
uj5u.com熱心網友回復:
如果> or <不存在,您可以跳過該行。嘗試這個 :
RichTextBox.AppendText("< 22:52:21:179 > Starting Argo Studio"
"\r\n" "<22:52:22:731 > Argo Studio has finished starting"
"\r\n" "<22:52:30:41 > Time to load commands: 00:00:00.00"
"\r\n" "< 22:52:30:48 > Created 'App 1'"
"\r\n" "23:0:4:320 Error - h88tzd: The source and destination are the same."
"\r\n" @"Source: 'C:\Users\user\Dropbox\PC\Desktop\...'."
"\r\n" @"Destination: 'C:\Users\user\Dropbox\PC\Desktop\...."
"\r\n" "More information: https:/");
for (int i = 0; i < RichTextBox.Lines.Length; i )
{
int indexStart = RichTextBox.Lines[i].IndexOf("<");
int indexEnd = RichTextBox.Lines[i].LastIndexOf(">");
if (indexStart < 0 || indexEnd < 0)
continue;
int baseIndex = RichTextBox.Text.IndexOf(RichTextBox.Lines[i]);
RichTextBox.Find(RichTextBox.Lines[i].Substring(indexStart 1, indexEnd-1));
RichTextBox.SelectionColor = Color.Gray;
}
然后結果:

希望這可以幫助!
uj5u.com熱心網友回復:
您可以使用Regex.Matches查找與時間戳匹配的所有文本部分。
每個Match物件都回傳Index找到匹配項的字符和Length匹配字串的字符。
此資訊可用于執行選擇。
文本是否被換行并不重要,因為整個文本都被考慮在內。
例如:
var matches = Regex.Matches(richTextBox.Text, @"<*\d :\d :\d :\d >*", RegexOptions.Multiline);
foreach (Match m in matches) {
richTextBox.SelectionStart = m.Index;
richTextBox.SelectionLength = m.Length;
richTextBox.SelectionColor = Color.Gray;
}
不清楚是否要在此處選擇時間戳:
Time to load commands: 00:00:00.00
如果您這樣做,則需要進行少量更改(例如,將模式設定為<*\d :\d :\d [:.]\d >*)
如果您只想要尖括號中的時間戳,那么<\d :\d :\d :\d >
uj5u.com熱心網友回復:
僅當該行包含<and>時,才格式化顏色
for (int i = 0; i < RichTextBox.Lines.Length; i )
{
if (RichTextBox.Lines[i].Contains("<") &&
RichTextBox.Lines[i].Contains(">"))
{
int indexStart = RichTextBox.GetFirstCharIndexFromLine(i);
int indexEnd = RichTextBox.Lines[i].Split(' ')[0].Length;
RichTextBox.Select(indexStart, indexEnd);
RichTextBox.SelectionColor = Color.Gray;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522153.html
標籤:C#表格
上一篇:檢查碰撞并防止重疊
下一篇:如何也獲得下載檔案鏈接描述?
