2

下午好。我是作为海报堆栈溢出的新手,但多年来一直引用它。我已经研究了我的这个问题大约 2 周,虽然我已经看到了接近的解决方案,但我仍然遇到了一个问题。

我正在编写一个 C# gui,它读取汇编代码文件并突出显示不同的文本项,以便通过另一个程序进行进一步处理。我的表单有一个显示文本的 RichTextBox。在下面的情况下,我试图在“;”的位置选择文本 直到行尾并将文本更改为红色。这是我正在使用的代码。

请注意:程序读取的文件长度不一致,并非所有行的格式都相同,所以我不能简单地搜索“;” 并对其进行操作。

在另一篇文章中,一位成员给出了 AppendText 的扩展方法,除了原始文本和我重新格式化的文本仍然存在之外,我已经可以完美地工作了。这是该站点的链接: 如何在richtextbox中使用多色

// Loop that it all runs in
Foreach (var line in inArray)
{   

  // getting the index of the ‘;’ assembly comments
  int cmntIndex = line.LastIndexOf(';');

  // getting the index of where I am in the rtb at this time.  
  int rtbIndex = rtb.GetFirstCharIndexOfCurrentLine();

  // just making sure I have a valid index
  if (cmntIndex != -1)
  {
    // using rtb.select to only select the desired 
    // text but for some reason I get it all    
    rtb.Select(cmntIndex + rtbIndex, rtb.SelectionLength);
    rtb.SelectionColor = Color.Red;
  }
}

以下是原始文件中的示例汇编代码,所有文本均为黑色:

;;TAG SOMETHING, SOMEONE START                          
    ASSEMBLY CODE       ; Assembly comments
    ASSEMBLY CODE       ; Assembly comments
    ASSEMBLY CODE       ; Assembly comments
;;TAG SOMETHING, SOMEONE FINISH

rtb.GetFirstCharIndexOfCurrentLine()被调用时,它会返回 RTB 的有效索引,我想如果我添加返回的值,line.LastIndexOf(';')我将能够只选择上面看起来像的文本; Assembly comments并将其变为红色。

确实发生的是整条线变成红色。

当我使用上面的 AppendText 方法时,我得到

ASSEMBLY CODE (this is black) ; Assembly comments (this is red) (the rest is black) ASSEMBLY CODE ; Assembly comments

黑色代码与重新着色的文本完全相同。在这种情况下,我需要知道如何清除 RTB 中的行和/或覆盖那里的文本。我尝试过的所有选项都会删除这些行。

Anywho,我敢肯定这很长,但我真的很难过,非常感谢您的建议。

4

2 回答 2

2

我希望我正确地理解了你。

这会遍历richtextbox.

根据要求使用 FOREACH 循环

要使用 foreach 循环,您只需要手动跟踪索引,如下所示:

// Index
int index = 0;

// Loop over each line
foreach (string line in richTextBox1.Lines)
{
    // Ignore the non-assembly lines
    if (line.Substring(0, 2) != ";;")
    {
        // Start position
        int start = (richTextBox1.GetFirstCharIndexFromLine(index) + line.LastIndexOf(";") + 1);

        // Length
        int length = line.Substring(line.LastIndexOf(";"), (line.Length - (line.LastIndexOf(";")))).Length;

        // Make the selection
        richTextBox1.SelectionStart = start;
        richTextBox1.SelectionLength = length;

        // Change the colour
        richTextBox1.SelectionColor = Color.Red;
    }

    // Increase index
    index++;
}

带 FOR 循环

// Loop over each line
for(int i = 0; i < richTextBox1.Lines.Count(); i++)
{
    // Current line text
    string currentLine = richTextBox1.Lines[i];

    // Ignore the non-assembly lines
    if (currentLine.Substring(0, 2) != ";;")
    {
        // Start position
        int start = (richTextBox1.GetFirstCharIndexFromLine(i) + currentLine.LastIndexOf(";") + 1);

        // Length
        int length = currentLine.Substring(currentLine.LastIndexOf(";"), (currentLine.Length - (currentLine.LastIndexOf(";")))).Length;

        // Make the selection
        richTextBox1.SelectionStart = start;
        richTextBox1.SelectionLength = length;

        // Change the colour
        richTextBox1.SelectionColor = Color.Red;
    }
}

红色突出显示的富文本示例

编辑:

重新阅读您的问题,我很困惑您是否想制作 ; 红色也是。

如果您确实从该行中删除了 +1:

int start = (richTextBox1.GetFirstCharIndexFromLine(i) + currentLine.LastIndexOf(";") + 1);
于 2015-06-30T23:53:56.580 回答
1
Private Sub RichTextBox1_Click(sender As Object, e As EventArgs) Handles RichTextBox1.Click
    Dim MyInt1 As Integer
    Dim MyInt2 As Integer
    ' Reset your RTB back color to white at each click
    RichTextBox1.SelectionBackColor = Color.White
    ' Define the nth first character number of the line you clicked
    MyInt1 = RichTextBox1.GetFirstCharIndexOfCurrentLine()
    ' use that nth to find the line number in the RTB
    MyInt2 = RichTextBox1.GetLineFromCharIndex(MyInt1)
    'Select the line using an array property of RTB (RichTextBox1.Lines())
    RichTextBox1.Select(MyInt1, RichTextBox1.Lines(MyInt2).Length)
    ' This line would be for font color change : RichTextBox1.SelectionColor = Color.Maroon
    ' This one changes back color :
    RichTextBox1.SelectionBackColor = Color.Yellow
End Sub

' rtb.select 方法有一些固有的错误 ' 如果换行,或者在“http”行上失败......可能更多。(我刚刚注意到上面代码中的默认 stackoverflow.com 字符颜色对于注释行和其他行不正确。)

于 2021-10-03T05:28:29.623 回答