dotnet / dotnet/wpf

RichTextBox.Caret = Run.ContentStart is not working, when Run.Text = " " (2 spaces)

Open
#4,693 2 comments 2 reactions 0 assignees View on GitHub
.NET Framework Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

* .NET Core Version: .NET 4.8
* Windows version: Windows 10 (1903)
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
* Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? No

**Problem description:**
I want to set the RichTextBox.Caret to the ContentStart of a Run-Element. This works, as long as the Run contains a char. But when the Run contains only 2 spaces then the Caret can not be set to Run.ContentStart. This makes no sense, because the Run is actually showing the spaces. See example in attachments

**Actual behavior:**
RichTextBox.Caret is positioned at Run.ContendEnd of the previous Run-Element

**Expected behavior:**
RichTextBox.Caret is positioned at Run.ContentStart - regardless of the containing chars

**Minimal repro:**
Use the attended .xaml as a flow-document and try to position the caret to the first Run-Element. I attached some screenshots which shows my Debug-Output. In example 1 (where the error happens) the Caret stays at Position 4. In example 2 it is working perfectly.

Private Sub SetRichTextBoxCarret(thisRTB As RichTextBox, thisTP As TextPointer, Optional additionalOffset As Int32 = 0)
Debug.WriteLine("")
Debug.WriteLine(String.Format("SetRichTextBoxCarret - TextPointerOffset: {0}", thisRTB.Document.ContentStart.GetOffsetToPosition(thisTP)))
Debug.WriteLine(String.Format("SetRichTextBoxCarret - TextPointer.LogicalDirection: {0}", thisTP.LogicalDirection))
Debug.WriteLine(String.Format("SetRichTextBoxCarret - TextPointerContext(LogicalDirection.Forward): {0}", thisTP.GetPointerContext(LogicalDirection.Forward).ToString))
Debug.WriteLine(String.Format("SetRichTextBoxCarret - TextPointerContext(LogicalDirection.Backward): {0}", thisTP.GetPointerContext(LogicalDirection.Backward).ToString))

Debug.WriteLine(String.Format("SetRichTextBoxCarret - CaretOffset: {0}", thisRTB.Document.ContentStart.GetOffsetToPosition(thisRTB.CaretPosition)))
Debug.WriteLine(String.Format("SetRichTextBoxCarret - Caret.LogicalDirection: {0}", thisRTB.CaretPosition.LogicalDirection))
Debug.WriteLine(String.Format("SetRichTextBoxCarret - CaretContext(LogicalDirection.Forward): {0}", thisRTB.CaretPosition.GetPointerContext(LogicalDirection.Forward).ToString))
Debug.WriteLine(String.Format("SetRichTextBoxCarret - CaretContext(LogicalDirection.Backward): {0}", thisRTB.CaretPosition.GetPointerContext(LogicalDirection.Backward).ToString))

Dim myDirection = If(CBool(thisRTB.CaretPosition.LogicalDirection), LogicalDirection.Backward, LogicalDirection.Forward)
Debug.WriteLine(String.Format("SetRichTextBoxCarret - myDirection: {0}", myDirection.ToString))

Dim myPosition = thisRTB.Document.ContentStart.GetPositionAtOffset(thisRTB.Document.ContentStart.GetOffsetToPosition(thisTP) + additionalOffset, myDirection)
thisRTB.CaretPosition = myPosition

Debug.WriteLine(String.Format("SetRichTextBoxCarret - CaretOffset after: {0}", thisRTB.Document.ContentStart.GetOffsetToPosition(thisRTB.CaretPosition)))
Debug.WriteLine(String.Format("SetRichTextBoxCarret - Caret after.LogicalDirection: {0}", thisRTB.CaretPosition.LogicalDirection))
Debug.WriteLine("")
End Sub

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.