Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I mark the cursor to stick in the character position it was at? (WPF RichTextBox)
    primarykey
    data
    text
    <p>I am using a WPF richtextbox to create a syntax highlighter for code provided in a textbox. I want to have it automatically adjust the rich textbox as I am typing, using a thread timer I reset the contents of the rich textbox. I am trying to keep the cursor where it is suppose to be. I don't loose any text characters during the parse (except line breaks). I don't know how to keep the caret position where the user has left it in the text. It defaults to the end of the document.</p> <p>I attempt to store the current caret position in the text and then set it to the document after I make my changes, however the error I receive is "Cannot set CaretPosition to be outside of RichTextBox." So I set the caret position to the bottom of the document.</p> <p>WPF, VB.net, .net Framework 4.0</p> <p>Here is the code.</p> <pre><code>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 &amp;= "&lt;Paragraph Margin=""0,0,0,0"" Padding=""0,0,0,2""&gt;" &amp; Line &amp; "&lt;/Paragraph&gt;" ElseIf Mid(Line, 1, 1) = "#" Then myText &amp;= "&lt;Paragraph Margin=""0,0,0,0"" Padding=""0,0,0,2""&gt;&lt;Run Foreground=""#FFFFA500""&gt;" &amp; Line &amp; "&lt;/Run&gt;&lt;/Paragraph&gt;" Else myText &amp;= "&lt;Paragraph Margin=""0,0,0,0"" Padding=""0,0,0,2""&gt;" &amp; Line &amp; "&lt;/Paragraph&gt;" End If count += 1 Next myText = Regex.Replace(myText, "\$(\w*)", "&lt;Run Foreground=""#FFFF0000""&gt;$$$1&lt;/Run&gt;") For Each var In MyVars myText = Regex.Replace(myText, "&lt;Run Foreground=""#FFFF0000""&gt;\" &amp; var &amp; "&lt;/Run&gt;", "&lt;Run Foreground=""#FF32CD32""&gt;" &amp; var &amp; "&lt;/Run&gt;") Next myText = myText.Replace("copy", "&lt;Run Foreground=""#FF87CEFA""&gt;copy&lt;/Run&gt;") myText = myText.Replace("delete", "&lt;Run Foreground=""#FF87CEFA""&gt;delete&lt;/Run&gt;") myText = myText.Replace("output", "&lt;Run Foreground=""#FF87CEFA""&gt;output&lt;/Run&gt;") myText = myText.Replace("clear", "&lt;Run Foreground=""#FF87CEFA""&gt;clear&lt;/Run&gt;") myText = "&lt;FlowDocument PagePadding=""5,0,5,0"" AllowDrop=""True"" NumberSubstitution.CultureSource=""User"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""&gt;" &amp; myText &amp; "&lt;/FlowDocument&gt;" 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload