简体   繁体   English

如何在 RichTextBox 中的两个字符之间显示 select 文本

[英]How to select text between two characters in a RichTextBox

I have a RichTextBox that logs information about my app.我有一个 RichTextBox,用于记录有关我的应用程序的信息。 Here is an example of what it may log:这是它可能记录的内容的示例:

<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'

The text between the < and the > is the time. <>之间的文本是时间。

I need to change the color of the time to gray.我需要将时间的颜色更改为灰色。

Previously, I did this:以前,我这样做:

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

However, this no longer works for me because now I have logs with multiple lines:但是,这不再对我有用,因为现在我有多行日志:

<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://

you can skip the line if > or < do not exist.如果> or <不存在,您可以跳过该行。 try this:试试这个:

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

then the result:然后结果:

在此处输入图像描述

hope this helps!希望这可以帮助!

You can use Regex.Matches to find all section of the text that match the time stamp.您可以使用Regex.Matches查找与时间戳匹配的所有文本部分。
Each Match object returns the char Index where the match was found and the Length of the matched string.每个Match object 返回找到匹配项的字符Index和匹配字符串的Length
This information can be used to perform a selection.此信息可用于执行选择。

It doesn't matter whether the text is wrapped or not, since the whole text is considered.文本是否换行并不重要,因为考虑了整个文本。

For example:例如:

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

Not exactly clear whether you want to select the time stamp here:不太清楚你是否想要 select 时间戳在这里:

Time to load commands: 00:00:00.00

small change required if you do (eg, set the pattern to <*\d+:\d+:\d+[:.]\d+>* )如果你这样做,需要做一些小改动(例如,将模式设置为<*\d+:\d+:\d+[:.]\d+>*

If you want only the time stamps in angle brackets, then <\d+:\d+:\d+:\d+>如果您只想要尖括号中的时间戳,则<\d+:\d+:\d+:\d+>

Only if the line contains < and > , then format color仅当该行包含<>时,才设置颜色格式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM