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

Undo/Redo for Programmatic Changes

1 Answer 139 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Sergey asked on 28 Nov 2011, 10:32 PM
I cannot get the following scenario to work.  I create InlineUIContainer and add a control to RTB.Document by clicking on a custom button.  Undo button does not remove this control.  Also, if I just programmatically create a new span with a hyperlink in it, undo does not work for that either.
Is there a way to accomplish that?

1 Answer, 1 is accepted

Sort by
Iva Toteva
Telerik team
answered on 30 Nov 2011, 07:22 PM
Hi Sergey,

The changes you apply to a document get registered in the history stack in the following two cases:
1. They are applied using methods or commands of RadRichTextBox;
2. They are applied using the UI (which eventually uses methods and commands of the rich text box as well).

When creating a document in code-behind or using methods of RadDocument to manipulate the content of a document, the history stack gets cleared. This is so because RadDocument's commands do not keep track of all relevant parameters. In a way, the history stack can be thought of a feature of the rich text box rather than the document. This is mentioned in this article.

However, most if not all modification of the document can be performed using RadRichTextBox's methods which does not result in clearing of the Undo/Redo stack. 
Inserting an InlineUIContainer can be done like this for example:
private void buttonAddTextBox_Click(object sender, RoutedEventArgs e)
    TextBox textBox = new TextBox() { Height = 100, Width = 200 };
    InlineUIContainer container = new InlineUIContainer() { UiElement = textBox, Height = 100, Width = 200 };

Inserting a Span can be done like this:
Span span = new Span("test history");

When it comes to hyperlinks, they are not to be inserted in a span, because spans cannot contain other document elements. The document structure is pictured in this article while the hyperlink functionality is described in greater detail here.
In a nutshell, a hyperlink can be inserted like this:
HyperlinkInfo info = new HyperlinkInfo()
    NavigateUri = "",
    Target = HyperlinkTargets.Blank,
    IsAnchor = false
this.radRichTextBox.InsertHyperlink(info, "RichTextBox demo");

I hope this helps.

Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Asked by
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Share this question