프로그래밍 방식으로 RichTextBox의 선택 변경
이 예제에서는 RichTextBox에서 현재 선택 영역을 프로그래밍 방식으로 변경하는 방법을 보여 줍니다. 이 선택은 사용자가 사용자 인터페이스를 사용하여 콘텐츠를 선택한 경우와 동일합니다.
RichTextBox 컨트롤에 대한 코드 예제
다음 XAML(Extensible Application Markup Language) 코드는 간단한 콘텐츠가 있는 RichTextBox라는 컨트롤을 설명합니다.
<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>
RichTextBox에서 텍스트를 선택하는 코드 예제
다음 코드는 사용자가 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);
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Namespace SDKSample
Partial Public Class ChangeSelectionProgrammaticaly
Inherits Page
' Change the current selection.
Private Sub ChangeSelection(ByVal sender As Object, ByVal args As RoutedEventArgs)
' Create two arbitrary TextPointers to specify the range of content to select.
Dim myTextPointer1 As TextPointer = myParagraph.ContentStart.GetPositionAtOffset(20)
Dim myTextPointer2 As TextPointer = myParagraph.ContentEnd.GetPositionAtOffset(-10)
' Programmatically change the selection in the RichTextBox.
richTB.Selection.Select(myTextPointer1, myTextPointer2)
End Sub
End Class
End Namespace
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기