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

Format painter. How to change selected text style?

5 Answers 168 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Tom
Top achievements
Rank 1
Tom asked on 14 Nov 2017, 01:16 PM

Hi,

I want to change selected text style. I don't have problem with change one word or line but i don't know how to select exact text and change style.

Below is code how i change style selected line.

private void Editor_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {      

                var normalStyle = editor.Document.StyleRepository[HeadingName];
                var position = new DocumentPosition(editor.Document.CaretPosition);
                position.MoveToCurrentLineStart();
                editor.Document.Selection.AddSelectionStart(position);
                position.MoveToCurrentLineEnd();
                editor.Document.Selection.AddSelectionEnd(position);

                editor.ChangeSpanStyle(normalStyle);

       }

I don't find any solution to solve this problem.

Any suggestion?

5 Answers, 1 is accepted

Sort by
0
Rick
Top achievements
Rank 1
answered on 15 Nov 2017, 11:30 AM

Hi Tom,

I am also looking to implement format painter. Do you have any suggestions? Is it possible for you to share the code for getting the style when clicking on "Format painter" button?

Thanks

 

0
Tom
Top achievements
Rank 1
answered on 15 Nov 2017, 11:51 AM

 

code behind:

Format painter button is RadRibbonToggleButton

 

public Span SpanCopy { get; private set; }
public string HeadingName { get; private set; }

private void FormatPainterButton_OnChecked(object sender, RoutedEventArgs e)
        {
            if (SpanCopy == null)
            {
                SpanCopy = editor.Document.CaretPosition.GetCurrentSpanBox().AssociatedSpan;
                var paragraph = SpanCopy.Parent as Paragraph;
                HeadingName = paragraph.Style.Name;
            }
        }

private void FormatPainterButton_OnUnchecked(object sender, RoutedEventArgs e)
        {
            SpanCopy = null;
            HeadingName = null;
        }

private void Editor_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (SpanCopy != null)
            {
                editor.Document.Selection.Clear();

                var normalStyle = editor.Document.StyleRepository[HeadingName];
                var position = new DocumentPosition(editor.Document.CaretPosition);

                position.MoveToCurrentLineStart();
                editor.Document.Selection.AddSelectionStart(position);
                position.MoveToCurrentLineEnd();
                editor.Document.Selection.AddSelectionEnd(position);


                editor.ChangeSpanStyle(normalStyle);
                editor.ChangeTextForeColor(SpanCopy.ForeColor);
                editor.ChangeFontSize(SpanCopy.FontSize);
                editor.ChangeFontStyle(SpanCopy.FontStyle);
                editor.ChangeFontFamily(SpanCopy.FontFamily);

                editor.ChangeFontWeight(SpanCopy.FontWeight);

                formatPainterButton.IsChecked = false;
            }
            SpanCopy = null;
            HeadingName = null;
        }

xaml:

      <telerik:DocumentRuler Grid.Row="1">
            <telerik:RadRichTextBox Name="editor"                                   
                                    LayoutMode="Paged" MouseLeftButtonUp="Editor_OnMouseLeftButtonUp" />
        </telerik:DocumentRuler>

 

 

position.MoveToCurrentLineStart(); and position.MoveToCurrentLineEnd(); - You can change position to current word, line, paragraph.. but i don't know how to change select exact text


 

0
Rick
Top achievements
Rank 1
answered on 16 Nov 2017, 04:56 AM
Thanks Tom. Our approaches are similar. We will let you know after we have made some progress in implementing the format painter (just like word).
0
Anna
Telerik team
answered on 16 Nov 2017, 01:24 PM
Hello Tom,

Thank you for your question and for providing your code.

I am happy to say that the feature is included in our backlog and we are hoping to implement it in the near future. You can find a link to the item in our portal here.

As to your question, shouldn't the format painter take the already existing selection and set the format properties to it? In other words you wouldn't have to change the selection, unless it is empty and in that case you would only need to select the current word. I would change the implementation to something along these lines:

private void Editor_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (SpanCopy != null)
    {
        var normalStyle = editor.Document.StyleRepository[HeadingName];
        var position = new DocumentPosition(editor.Document.CaretPosition);
 
        if (editor.Document.Selection.IsEmpty)
        {
            this.PaintFormatOnEmptySelection(position, normalStyle);
        }
        else
        {
            this.PaintFormatOnCurrentSelection(normalStyle);
        }
 
        formatPainterButton.IsChecked = false;
    }
 
    SpanCopy = null;
    HeadingName = null;
}
 
private void PaintFormatOnEmptySelection(DocumentPosition position, StyleDefinition style)
{
    position.MoveToCurrentWordStart();
    editor.Document.Selection.AddSelectionStart(position);
    position.MoveToCurrentWordEnd();
    editor.Document.Selection.AddSelectionEnd(position);
 
    this.PaintProperties(style);
 
    editor.Document.Selection.Clear();
}
 
private void PaintFormatOnCurrentSelection(StyleDefinition style)
{
    this.PaintProperties(style);
}
 
private void PaintProperties(StyleDefinition style)
{
    editor.ChangeSpanStyle(style);
    editor.ChangeForeColor(SpanCopy.ForeColor);
    editor.ChangeFontSize(SpanCopy.FontSize);
    editor.ChangeFontStyle(SpanCopy.FontStyle);
    editor.ChangeFontFamily(SpanCopy.FontFamily);
 
    editor.ChangeFontWeight(SpanCopy.FontWeight);
}

Please let me know if I understood your scenario correctly.

Regards,
Anna
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Tom
Top achievements
Rank 1
answered on 17 Nov 2017, 06:41 AM

Hi Anna,

Thanks for improve my code. This is exactly what i needed.

Regards
Tags
RichTextBox
Asked by
Tom
Top achievements
Rank 1
Answers by
Rick
Top achievements
Rank 1
Tom
Top achievements
Rank 1
Anna
Telerik team
Share this question
or