3

我试图在我的 RichTextBox 中突出显示一行。我的尝试是获取该行在文本中的位置,然后创建一个代表text.Substring(offset, word.Length). 但不知何故,RichTextBox 只突出显示前一行的最后 2 个字符和实际行的一些字符或只是实际行的一部分。我目前的方法是这样的:

    public void SelectLine(string text)
    {
        int i = new TextRange(editor.Document.ContentStart, editor.Document.ContentEnd).Text.IndexOf(text);
        TextPointer start = editor.Document.ContentStart.GetPositionAtOffset(i);
        TextPointer end = start.GetPositionAtOffset(text.Length);

        TextRange r = new TextRange(start, end);

        if (r != null)
        {
            r.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Red);
            r.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.White);
        }
    }

这就是我尝试选择第二行时的样子:

在此处输入图像描述

你有什么想法,为什么会这样?

编辑: 我目前的方法包括 WPF RichTextBox。

4

2 回答 2

2

经过大量搜索,我终于找到了这个解决方案。我遍历文档的块,同时数到给定的行。我不在乎是否突出显示整个段落或仅突出显示行,因为我编写了一个小型代码编辑器,它应该显示错误并在给定的源代码中突出显示它。这是我的突出显示代码:

    public void SelectLine(int line)
    {
        int c = 0;
        TextRange r;

        foreach (var item in editor.Document.Blocks)
        {
            if (line == c)
            {
                r = new TextRange(item.ContentStart, item.ContentEnd);
                if (r.Text.Trim().Equals(""))
                {
                    continue;
                }
                r.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Red);
                r.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.White);
                return;
            }
            c++;
        }
    }

它遍历文档的块并检查给定的块是否为空。如果是这样,那么将研究下一个块。

于 2014-02-23T12:26:10.327 回答
0

经过大量研究,我找到了一个解决方案,这里有你所需要的!

 TextRange Mytextrange2;
 private void Selectline(int line)
    {
        // STEP1: set CaretPosition to fist position of richtextbox (Vietnamese: đưa Carretposition về vị trí đầu của richtextbox)
        TextPointer resetpos = richTextBox.Document.ContentStart;   
        richTextBox.CaretPosition = resetpos;

        // STEP2: get start Position of your line and get startpos of your line +1 (mean end positon of your need line) (Vietnammes: Lấy vị trí đầu và cuối của line)
        TextPointer startPos = richTextBox.CaretPosition.GetLineStartPosition(line);
        TextPointer endPos = richTextBox.CaretPosition.GetLineStartPosition(line + 1);

        // STEP3: return if Error
        if (startPos == null) return;
        if (endPos == null) return;

        // STEP4: Clear properties of your previous line (i'm highlighting line by line of richtextbox, so i need to clear properties of previous line before apply new properties to next line)
// Xóa các thuộc tính áp dụng cho dòng trước, vì tôi bôi đen từng dòng trong richtextbox nên khi bôi đen dòng này tôi sẽ bỏ bôi đen dòng trước kia đi
        if (line != 0) // in this case, i clear properties when line != 0, ofcoursce you can change it (trong trường hợp dòng 0, tôi không xóa dòng trước vì trước nó không có dòng nào cả)
        {
            Mytextrange2.ClearAllProperties(); 
            //Mytextrange2.ApplyPropertyValue(TextElement.BackgroundProperty,Brushes.DarkGray);
            Mytextrange2.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.DarkGray);
        }

        // STEP5: Select new textrang and apply new properties to your new line // Dùng textrange để chọn dòng cần bôi đen, sau đó add các thuộc tính vào
        Mytextrange2.Select(startPos, endPos); // select range of text to apply new properties
        Mytextrange2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);  
        Mytextrange2.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.DarkGray);
        Mytextrange2.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);

        // STEP6: scroll richtextbox to next 14th line to make reasonable view (if imposible). // scroll đến 14 dòng sau dòng được chọn, để dòng được chọn ở giữa khung màn hình, bạn có thể chọn dòng khác
        richTextBox.CaretPosition = startPos; // Set CaretPosition to selected line
        startPos = richTextBox.CaretPosition.GetLineStartPosition(14); //(find position of next 14th line to scroll ) Change value 14 to have more reasonable view
        if (startPos != null)  richTextBox.CaretPosition = startPos;   // Set caretposition to position of next 14 lines
        richTextBox.Focus();   // scroll richtextbox to caretposition
    }
  private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // Mytextrange2 là biến toàn cục muốn dùng để thực hiện xóa properties của dòng trước, nên tôi phải khởi tạo ở 1 chỗ khác ngoài hàm Selectline
// Initialize Mytextrang2
        Mytextrange2 = new TextRange(richTextBox.Document.ContentStart,richTextBox.Document.ContentEnd);
    }
于 2018-01-19T16:16:56.147 回答