--- title: "Change Selection in a RichTextBox Programmatically" ms.date: "03/30/2017" ms.custom: devdivchpfy22 description: Learn how to change selection in a RichTextBox programmatically. dev_langs: - "csharp" - "vb" helpviewer_keywords: - "changing selections in a text box [WPF]" - "changing selections in a RichTextBox [WPF]" ms.assetid: f1213205-1ad7-4cd2-b115-460173cc5aa3 --- # Change Selection in a RichTextBox Programmatically This example shows how to programmatically change the current selection in a . This selection is the same as if the user had selected the content by using the user interface. ## Code example for a RichTextBox control The following Extensible Application Markup Language (XAML) code describes a named control with simple content. [!code-xaml[RichTextBoxMiscSnippets_snip#ChangeSelectionProgrammaticalyExampleWholePage](~/samples/snippets/csharp/VS_Snippets_Wpf/RichTextBoxMiscSnippets_snip/CSharp/ChangeSelectionProgrammaticaly.xaml#changeselectionprogrammaticalyexamplewholepage)] ## Code example to select text from a RichTextBox The following code programmatically selects some arbitrary text when the user clicks inside the . [!code-csharp[RichTextBoxMiscSnippets_snip#ChangeSelectionProgrammaticalyCodeExampleWholePage](~/samples/snippets/csharp/VS_Snippets_Wpf/RichTextBoxMiscSnippets_snip/CSharp/ChangeSelectionProgrammaticaly.xaml.cs#changeselectionprogrammaticalycodeexamplewholepage)] [!code-vb[RichTextBoxMiscSnippets_snip#ChangeSelectionProgrammaticalyCodeExampleWholePage](~/samples/snippets/visualbasic/VS_Snippets_Wpf/RichTextBoxMiscSnippets_snip/VisualBasic/ChangeSelectionProgrammaticaly.xaml.vb#changeselectionprogrammaticalycodeexamplewholepage)] ## See also - [RichTextBox Overview](richtextbox-overview.md) - [TextBox Overview](textbox-overview.md)