This is a migrated thread and some comments may be shown as answers.

Annotation at start of paragraph changes tab behavior

4 Answers 128 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Ted
Top achievements
Rank 1
Ted asked on 17 Mar 2015, 12:26 AM
When the cursor position is at the start of a paragraph, the tab/shift-tab keys are used to increase/decrease the indent.  If an annotation, such as a bookmark, is placed at the start of the paragraph, this behavior changes and a tab is inserted instead.

We use annotations to mark areas of our documents and this causes inconsistent behavior.  What's the best way to resolve this?

Here is an example of some XAML that has 2 lines that work and 1 line that fails because it has a bookmark at the start of the paragraph:
<t:Paragraph LeftIndent="0">
  <t:Span Text="This paragraph can be indented using the tab key." />
</t:Paragraph>
<t:Paragraph LeftIndent="0">
  <t:BookmarkRangeStart AnnotationID="1" Name="MyBookmark" />
  <t:BookmarkRangeEnd AnnotationID="1" />
  <t:Span Text="This paragraph cannot be indented using the tab key." />
</t:Paragraph>
<t:Paragraph>
  <t:Span Text="This paragraph can be indented using the tab key." />
</t:Paragraph>
<t:Paragraph />

Thank You

4 Answers, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 18 Mar 2015, 03:06 PM
Hi Ted,

This behavior is caused by a specificity of the Bookmark annotation. The position before it is special (non-navigable, due to the value of the SkipPositionBefore property) - meaning that in the case when paragraph starts with Bookmark, you cannot put the caret in the beginning of the paragraph - as the caret will always move after the Bookmark.

On the other hand, the behavior when pressing Tab key is different in the different contexts, and when the caret is not at the beginning of the paragraph, Tab character ("\t") is inserted instead of changing the left indent. I will log the issue in our backlog, but it will be with low priority as it is more of a small usability problem. You can track the issue here:
Pressing Tab key when the caret is at the beginning of a Paragraph which starts with Bookmark do not change the indent

You have some possible workarounds:
- Bind the "Tab" key directly to IncrementParagraphLeftIndent command. This way you will lose the before-mentioned logic from TabForward command for inserting Tab character, moving to next table cell, etc.; and pressing Tab will always  change the left indent.

- Use RadRichTextBox.CommandExecuting event, and in this specific case cancel the insertion of the tab character and execute the IncrementParagraphLeftIndent:

void radRichTextBox_CommandExecuting(object sender, Telerik.Windows.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
{
    if (e.Command is InsertTextCommand && e.CommandParameter == "\t")
    {
        if (IsAtTheBeginningOfParagraph(this.radRichTextBox.Document.CaretPosition))
        {
            e.Cancel = true;
            this.radRichTextBox.IncrementParagraphLeftIndent();
        }
    }
}
 
private bool IsAtTheBeginningOfParagraph(DocumentPosition position)
{
    var currentParagraph = position.GetCurrentParagraphBox().AssociatedParagraph;
 
    DocumentPosition testPosition = new DocumentPosition(position);
 
    bool canMoveToPrevious = false;
 
    while ((canMoveToPrevious = testPosition.MoveToPrevious()) && testPosition.GetCurrentInline() is AnnotationRangeMarkerBase)
    {
    }
 
    return !canMoveToPrevious || testPosition.GetCurrentParagraphBox().AssociatedParagraph != currentParagraph;
}

I hope this was helpful.

Regards,
Boby
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Ted
Top achievements
Rank 1
answered on 18 Mar 2015, 03:29 PM
Thanks Boby.  I'll explore those solutions.  Both sound like good workarounds.
0
Bilisim
Top achievements
Rank 1
answered on 19 Nov 2018, 06:20 AM

VB.net ??

 

0
Bilisim
Top achievements
Rank 1
answered on 19 Nov 2018, 06:23 AM

vb.Net

  Private Sub radRichTextBox_CommandExecuting(ByVal sender As Object, ByVal e As CommandExecutingEventArgs)
        If TypeOf e.Command Is InsertTextCommand AndAlso e.CommandParameter = vbTab Then

            If IsAtTheBeginningOfParagraph(Me.radRichTextEditor1.Document.CaretPosition) Then
                e.Cancel = True
                Me.radRichTextEditor1.IncrementParagraphLeftIndent()
            End If
        End If
    End Sub

    Private Function IsAtTheBeginningOfParagraph(ByVal position As DocumentPosition) As Boolean
        Dim currentParagraph = position.GetCurrentParagraphBox().AssociatedParagraph
        Dim testPosition As DocumentPosition = New DocumentPosition(position)
        Dim canMoveToPrevious As Boolean = False

        While (CSharpImpl.__Assign(canMoveToPrevious, testPosition.MoveToPrevious())) AndAlso TypeOf testPosition.GetCurrentInline() Is AnnotationRangeMarkerBase
        End While

        Return Not canMoveToPrevious OrElse testPosition.GetCurrentParagraphBox().AssociatedParagraph IsNot currentParagraph
    End Function

    Private Class CSharpImpl
        <Obsolete("Please refactor calling code to use normal Visual Basic assignment")>
        Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function
    End Class

Tags
RichTextBox
Asked by
Ted
Top achievements
Rank 1
Answers by
Boby
Telerik team
Ted
Top achievements
Rank 1
Bilisim
Top achievements
Rank 1
Share this question
or