# History

The `RadDocument` class can track the history of any actions taken against its content. In this way it allows an undo and redo functionality to be easily implemented. The history is implemented via the `DocumentHistory` class and the RadDocument exposes the `History` property of this type. The `RadRichTextBox` control automatically adds and removes items from the history, when its API methods get called, but you are allowed to manually work with the history as well.

> tip To learn more about the DocumentHistory API, read [here](http://www.telerik.com/help/wpf/t_telerik_windows_documents_history_documenthistory.html).

This topic will explain you how to:

* [Enable/Disable History](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/history#enable-disable-history)
* [Clear History](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/history#clear-history)
* [Undo/Redo Actions](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/history#undo-redo-actions)
* [Change History Depth](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/history#change-history-depth)
* [Preserve History Using RadDocumentEditor](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/history#preserve-history-using-raddocumenteditor)
* [UndoGroup](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/history#undogroup)

## Enable/Disable History

You can enable or disable the history for the RadDocument via the `Enabled` property of the DocumentHistory.

**Disable history**

```C#
	this.radRichTextBox.Document.History.Enabled = false;
```

```VB.NET
	Me.radRichTextBox.Document.History.Enabled = False
```

## Clear History

To clear the history you just have to call the `Clear` method of the DocumentHistory class.

**Clearing the history**

```C#
	this.radRichTextBox.Document.History.Clear();
```

```VB.NET
	Me.radRichTextBox.Document.History.Clear()
```

## Undo/Redo Actions

To undo and redo some actions, you can call the `Undo` and `Redo` methods of RadRichTextBox.

**Using the Redo and Undo methods of RadRichTextBox**

```C#
	private void UndoAction()
        {
            this.radRichTextBox.Undo();
        }
        private void RedoAction()
        {
            this.radRichTextBox.Redo();
        }
```

```VB.NET
	Private Sub UndoAction()
	 Me.radRichTextBox.Undo()
	End Sub
	Private Sub RedoAction()
	 Me.radRichTextBox.Redo()
	End Sub
```

## Change History Depth

To change the history capacity you have to set the desired value of the `Depth` property of the DocumentHistory. The default one is **1000**.

**Changing the history depth**

```C#
	this.radRichTextBox.Document.History.Depth = 500;
```

```VB.NET
	Me.radRichTextBox.Document.History.Depth = 500
```

## Preserve History Using RadDocumentEditor

The methods of the RadDocument class do not register in the undo/redo stack. Once a method is invoked, the history stack will be cleared and the users will no longer be able to undo/redo previous actions. Due to this and for some other factors, the RadDocumentEditor class was introduced. You can find detailed information about it [here](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/getting-started/getting-started#raddocumenteditor).

## UndoGroup

The `RadDocumentEditor` control allows you to group several methods so that they are added to the Undo/Redo stack as a single item. To do so, you can use the `BeginUndoGroup` and `EndUndoGroup` methods.

**Using the BeginUndoGroup and EndUndoGroup methods**

```C#
	documentEditor.BeginUndoGroup(); 
	
	if (documentEditor.Document.CaretPosition.IsPositionInsideTable) 
	{ 
	    documentEditor.InsertTableRow(); 
	    documentEditor.InsertTableRow(); 
	    documentEditor.InsertTableRow(); 
	    documentEditor.Document.Selection.Clear(); 
	}

    documentEditor.EndUndoGroup("Insert three table rows"); 
```

```VB.NET
	documentEditor.BeginUndoGroup() 
	
	If documentEditor.Document.CaretPosition.IsPositionInsideTable Then 
		documentEditor.InsertTableRow() 
		documentEditor.InsertTableRow() 
		documentEditor.InsertTableRow() 
		documentEditor.Document.Selection.Clear() 
	End If 
	
	documentEditor.EndUndoGroup("Insert three table rows") 
```

To cancel the execution of the undo group and prevent it from being recorded to the history, you can use the `CancelUndoGroup` method.

**Using the CancelUndoGroup method**

```C#
	documentEditor.BeginUndoGroup(); 
	
	if (documentEditor.Document.CaretPosition.IsPositionInsideTable) 
	{ 
		documentEditor.InsertTableRow(); 
		documentEditor.InsertTableRow(); 
		documentEditor.InsertTableRow(); 
		documentEditor.Document.Selection.Clear(); 
	}

documentEditor.CancelUndoGroup(); 
```

```VB.NET
	documentEditor.BeginUndoGroup() 
	
	If documentEditor.Document.CaretPosition.IsPositionInsideTable Then 
		documentEditor.InsertTableRow() 
		documentEditor.InsertTableRow() 
		documentEditor.InsertTableRow() 
		documentEditor.Document.Selection.Clear() 
	End If 
	
	documentEditor.CancelUndoGroup() 
```

One thing to note here is that it is not possible to remove some actions from the undo history altogether, i.e. you cannot perform an action without it getting registered in the Undo/Redo stack. In most cases, however, this is sufficient, as you can group the operations that you do not want to name and show explicitly to the end user with the ones that have been user-initiated and are expected by the person modifying the content of RadRichTextBox.

## See Also

* [Selection](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/selection)
* [Positioning](https://www.telerik.com/products/wpf/documentation/controls/radrichtextbox/features/positioning)
