如何标记光标以固定在它所处的角色位置? (WPF RichTextBox)

时间:2011-03-07 15:45:08

标签: wpf vb.net richtextbox

我正在使用WPF richtextbox为文本框中提供的代码创建语法高亮显示。我想让它在我输入时自动调整富文本框,使用线程计时器我重置富文本框的内容。我试图将光标保持在原来的位置。在解析期间我没有丢失任何文本字符(换行符除外)。我不知道如何将插入位置保留在用户将其留在文本中的位置。它默认为文档的末尾。

我尝试将当前插入符号位置存储在文本中,然后在我进行更改后将其设置为文档,但是我收到的错误是“无法将CaretPosition设置为在RichTextBox之外”。所以我将插入位置设置在文档的底部。

WPF,VB.net,.net Framework 4.0

这是代码。

Public Sub FormatText()

    If IsNothing(rtfContent.Document) Then
        Exit Sub
    End If

    Me.rtfContent.IsEnabled = False
    Me.rtfContent.Refresh()
    Me.Refresh()

    System.Threading.Thread.Sleep(50)

    Dim curRange As TextRange = New TextRange(rtfContent.Document.ContentStart, rtfContent.CaretPosition)

    Dim caretBefore As TextPointer = rtfContent.CaretPosition
    caretBefore = New TextRange(caretBefore, caretBefore.DocumentEnd).Start

    Dim caretBeforeOffset As Integer = rtfContent.Document.ContentStart.GetOffsetToPosition(caretBefore)

    Dim docRange As TextRange = New TextRange(rtfContent.Document.ContentStart, rtfContent.Document.ContentEnd)
    docRange.ClearAllProperties()

    Dim myText As String = docRange.Text
    Dim Lines As List(Of String) = Split(myText, vbCrLf).ToList
    Dim MyBlocks As New List(Of Block)
    Dim count As Integer = 1
    MyVars.Clear()
    myText = ""

    For Each Line In Lines
        If count = Lines.Count Then
            Exit For
        End If

        If Mid(Line, 1, 1) = "$" Then
            Dim words As String() = Split(Line, " ", 2)
            MyVars.Add(words(0))
            myText &= "<Paragraph Margin=""0,0,0,0"" Padding=""0,0,0,2"">" & Line & "</Paragraph>"
        ElseIf Mid(Line, 1, 1) = "#" Then
            myText &= "<Paragraph Margin=""0,0,0,0"" Padding=""0,0,0,2""><Run Foreground=""#FFFFA500"">" & Line & "</Run></Paragraph>"
        Else
            myText &= "<Paragraph Margin=""0,0,0,0"" Padding=""0,0,0,2"">" & Line & "</Paragraph>"
        End If

        count += 1
    Next

    myText = Regex.Replace(myText, "\$(\w*)", "<Run Foreground=""#FFFF0000"">$$$1</Run>")

    For Each var In MyVars
        myText = Regex.Replace(myText, "<Run Foreground=""#FFFF0000"">\" & var & "</Run>", "<Run Foreground=""#FF32CD32"">" & var & "</Run>")
    Next

    myText = myText.Replace("copy", "<Run Foreground=""#FF87CEFA"">copy</Run>")
    myText = myText.Replace("delete", "<Run Foreground=""#FF87CEFA"">delete</Run>")
    myText = myText.Replace("output", "<Run Foreground=""#FF87CEFA"">output</Run>")
    myText = myText.Replace("clear", "<Run Foreground=""#FF87CEFA"">clear</Run>")

    myText = "<FlowDocument PagePadding=""5,0,5,0"" AllowDrop=""True"" NumberSubstitution.CultureSource=""User"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">" & myText & "</FlowDocument>"

    Me.rtfContent.Document.Blocks.Clear()
    Me.rtfContent.Document = TryCast(XamlReader.Parse(myText), FlowDocument)

    caretBefore = caretBefore.DocumentStart.GetPositionAtOffset(caretBeforeOffset, LogicalDirection.Forward)

    Try
        Me.rtfContent.CaretPosition = caretBefore
    Catch
        Me.rtfContent.CaretPosition = rtfContent.Document.ContentEnd
    End Try

    Me.rtfContent.IsEnabled = True
    Me.rtfContent.Refresh()
    Me.Refresh()

End Sub

1 个答案:

答案 0 :(得分:1)

我有一个可能会有所帮助的想法。 Point GetPositionFromCharIndex(int index)PointToClientPointToScreen函数有帮助吗?您可以存储鼠标光标所在的char索引,并在生成代码后将其放回上述位置。您可以找到这些函数的使用here