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
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
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
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
Hi Anna,
Thanks for improve my code. This is exactly what i needed.
Regards