TextRange.Select Method

Updates the current selection, taking two TextPointer positions to indicate the updated selection.

Namespace:  System.Windows.Documents
Assembly:  PresentationFramework (in PresentationFramework.dll)

public void Select(
	TextPointer position1,
	TextPointer position2
)

Parameters

position1
Type: System.Windows.Documents.TextPointer
A fixed anchor position that marks one end of the updated selection.
position2
Type: System.Windows.Documents.TextPointer
A movable position that marks the other end of the updated selection.

ExceptionCondition
ArgumentException

Occurs when position1 and position2 are not positioned within the same document.

ArgumentNullException

Occurs when position1 or position2 is null.

A TextRange is formed from a selection between two positions indicated by TextPointers. One of these positions (indicated by position1) is fixed with respect to the selection, while the other position (indicated by position2) is movable. This is similar to how a selection made by a user using the mouse or keyboard behaves.

The actual ends of the new selection may be adjusted to match any selection heuristics that are applicable to the document that contains the new current selection.

This example shows how to programmatically change the current selection in a RichTextBox. This selection is the same as if the user had selected the content by using the user interface.

The following Extensible Application Markup Language (XAML) code describes a named RichTextBox control with simple content.


<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.ChangeSelectionProgrammaticaly" >

  <StackPanel>
    <RichTextBox GotMouseCapture="ChangeSelection" Name="richTB">
      <FlowDocument>
        <Paragraph Name="myParagraph">
          <Run>
            When the user clicks in the RichTextBox, the selected
            text changes programmatically.
          </Run>
        </Paragraph>
      </FlowDocument>
    </RichTextBox>
  </StackPanel>

</Page>


The following code programmatically selects some arbitrary text when the user clicks inside the RichTextBox.


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace SDKSample
{
    public partial class ChangeSelectionProgrammaticaly : Page
	{

        // Change the current selection.
        void ChangeSelection(Object sender, RoutedEventArgs args)
        {
            // Create two arbitrary TextPointers to specify the range of content to select.
            TextPointer myTextPointer1 = myParagraph.ContentStart.GetPositionAtOffset(20);
            TextPointer myTextPointer2 = myParagraph.ContentEnd.GetPositionAtOffset(-10);

            // Programmatically change the selection in the RichTextBox.
            richTB.Selection.Select(myTextPointer1, myTextPointer2);
        }
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft