Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I copied the following code from the MSDN documentation of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.textrange.select.aspx" rel="nofollow noreferrer">TextRange.Select</a> method, replaced the RichTextBox with a FlowDocumentScrollViewer and it works as expected. How did you define your TextPointers? That <em>seems</em> to be the most probable cause of your problem.</p> <p>UPDATE: I updated my code to include your selection algorithm and it still works. The only thing I'm aware of doing different is the "break" after selecting. Otherwise it'd select from the beginning of the first occurence of searchText until after the end of the last occurence. Other than that I could imagine that your searchText might not be included in your document at all (perhaps a casing issue?), but that's just guessing. Did you debug your code? Are the TextPointers valid (not null etc.) when you try to select the text?</p> <p>XAML: </p> <pre><code>&lt;FlowDocumentScrollViewer GotMouseCapture="richTB_GotMouseCapture" Name="richTB"&gt; &lt;FlowDocument&gt; &lt;Paragraph Name="myParagraph"&gt; &lt;Run&gt; When the user clicks in the RichTextBox, the selected text changes programmatically. &lt;/Run&gt; &lt;/Paragraph&gt; &lt;/FlowDocument&gt; &lt;/FlowDocumentScrollViewer&gt; </code></pre> <p>Code:</p> <pre><code>private void richTB_GotMouseCapture(object sender, MouseEventArgs e) { string searchText = "text"; TextPointer pointer = richTB.Document.ContentStart; while (pointer != null) { if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { string textRun = pointer.GetTextInRun(LogicalDirection.Forward); // where textRun is the text in the flowDocument // and searchText is the text that is being searched for int indexInRun = textRun.IndexOf(searchText); if (indexInRun &gt;= 0) { TextPointer startPos = pointer.GetPositionAtOffset(indexInRun); TextPointer endPos = pointer.GetPositionAtOffset(indexInRun + searchText.Length); richTB.Selection.Select(startPos, endPos); break; } } pointer = pointer.GetNextContextPosition(LogicalDirection.Forward); } } </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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