windowsforms-18077-controls-and-libraries-rich-text-editor-rich-edit-control-document-comments.md
The RichEditControl allows you to add comments to documents. You can add, edit, and remove comments in code and from the UI.
Use the following API to attach a comment to a document range:
| Member | Description |
|---|---|
| Paragraph.Range | Retrieves the paragraph’s range. |
| SubDocument.FindAll | Finds all document ranges that contain the expression or text string. |
| SubDocument.Comments | Provides access to the document’s Comment collection. |
| CommentCollection.Create | Creates a new item in the CommentCollection. |
Note
When you add a new comment, the CommentOptions.Visibility property set to Hidden changes its value to Visible.
The following creates an empty comment associated with the following phrase: “an extensive problem in hardware and architecture is the construction of the emulation of checksums”.
// The target range is the specific phrase in the introduction's first paragraph
DocumentRange[] foundRanges = richEditControl.Document.FindAll("an extensive problem in hardware and architecture is the construction of the emulation of checksums", SearchOptions.None);
if (foundRanges.Length > 0)
{
// Create a new comment
Comment comment = richEditControl.Document.Comments.Create(foundRanges[0], "Johnson Alphonso D", new DateTime(2014, 4, 25));
}
' The target range is the specific phrase in the introduction's first paragraph
Dim foundRanges() As DocumentRange = richEditControl.Document.FindAll("an extensive problem in hardware and architecture is the construction of the emulation of checksums", SearchOptions.None)
If foundRanges.Length > 0 Then
' Create a new comment
Dim comment As Comment = richEditControl.Document.Comments.Create(foundRanges(0), "Johnson Alphonso D", New Date(2014, 4, 25))
End If
The code snippet below demonstrates how to create a nested comment. To do that, pass the parent content as the CommentCollection.Create method’s parameter.
Document document = richEditControl.Document;
if (document.Comments.Count > 0)
{
// Create a new comment nested to the second comment in the collection
Comment nestedComment = document.Comments.Create("Brian Zetc", DateTime.Now, document.Comments[1]);
// Add text to the newly created comment
SubDocument nestedCommentDocument = nestedComment.BeginUpdate();
DocumentRange textRange = nestedCommentDocument.InsertText(nestedCommentDocument.CreatePosition(0),
"Suffix trees are comprehensively reviewed in Wikipedia");
nestedComment.EndUpdate(nestedCommentDocument);
}
Dim document As Document = richEditControl.Document
If document.Comments.Count > 0 Then
' Create a new comment nested to the second comment in the collection
Dim nestedComment As Comment = document.Comments.Create("Brian Zetc", Date.Now, document.Comments(1))
' Add text to the newly created comment
Dim nestedCommentDocument As SubDocument = nestedComment.BeginUpdate()
Dim textRange As DocumentRange = nestedCommentDocument.InsertText(nestedCommentDocument.CreatePosition(0), "Suffix trees are comprehensively reviewed in Wikipedia")
nestedComment.EndUpdate(nestedCommentDocument)
End If
Use the API from the table below to edit the comment’s content.
| Member | Description |
|---|---|
| Comment.BeginUpdate | Opens the comment for modification. Use the SubDocument object returned by the BeginUpdate method to add and edit comment content. |
| SubDocument.InsertText | Inserts a text string into the specified position. |
| TableCollection.Create | Inserts a table into a document position. |
| ShapeCollection.InsertPicture | Inserts a floating picture into the specified position position. |
| DocumentImageCollection.Insert | Inserts an inline image in the specified position. |
| ShapeCollection.InsertTextBox | Inserts a text box in the specified position. |
| Comment.EndUpdate | Finalizes the comment update. |
The following code sample adds text to the empty comment:
Document document = richEditControl.Document;
int commentCount = document.Comments.Count;
if (commentCount > 0)
{
// The target comment is the first one
Comment comment = document.Comments[0];
if (comment != null)
{
// Open the comment for modification
SubDocument commentDocument = comment.BeginUpdate();
// Add text to the comment range
commentDocument.InsertText(commentDocument.CreatePosition(0),
"J. Taylor, Enabling Voice - over - IP and RAID with sofa, in Proceedings of NOSSDAV, Oct. 1994.\r\n" +
@"R.Tarjan, S.Shenker, J.Gray, A.Einstein, Q.Thomas, and X.Sato, ""Deconstructing operating systems with flanchedripper"", in Proceedings of INFOCOM, Mar. 2000.");
// End the comment update
comment.EndUpdate(commentDocument);
}
}
Dim document As Document = richEditControl.Document
Dim commentCount As Integer = document.Comments.Count
If commentCount > 0 Then
' The target comment is the first one
Dim comment As Comment = document.Comments(0)
If comment IsNot Nothing Then
' Open the comment for modification
Dim commentDocument As SubDocument = comment.BeginUpdate()
' Add text to the comment range
commentDocument.InsertText(commentDocument.CreatePosition(0), "J. Taylor, Enabling Voice - over - IP and RAID with sofa, in Proceedings of NOSSDAV, Oct. 1994." & ControlChars.CrLf & "R.Tarjan, S.Shenker, J.Gray, A.Einstein, Q.Thomas, and X.Sato, ""Deconstructing operating systems with flanchedripper"", in Proceedings of INFOCOM, Mar. 2000.")
' End the comment update
comment.EndUpdate(commentDocument)
End If
End If
Use the following members to change the comment parameters (author, date, etc.):
| Member | Description |
|---|---|
| Comment.BeginUpdate | Opens the comment for editing. |
| Comment.Author | Specifies the comment’s author. |
| Comment.Date | Specifies the comment’s last modified date. |
| Comment.Name | Specifies the comment’s name. |
| Comment.EndUpdate | Finalizes the comment update. |
The code snippet below changes the comment’s name, date and author.
Document document = richEditControl.Document;
int commentCount = document.Comments.Count;
if (commentCount > 0)
{
document.BeginUpdate();
// The target is the second last comment
Comment comment = document.Comments[document.Comments.Count - 1];
// Change the comment's name, author and creation date
comment.Name = "Reference";
comment.Date = DateTime.Now;
comment.Author = "Vicars Annie";
document.EndUpdate();
}
Dim document As Document = richEditControl.Document
Dim commentCount As Integer = document.Comments.Count
If commentCount > 0 Then
document.BeginUpdate()
' The target is the second last comment
Dim comment As Comment = document.Comments(document.Comments.Count - 1)
' Change the comment's name, author and creation date
comment.Name = "Reference"
comment.Date = Date.Now
comment.Author = "Vicars Annie"
document.EndUpdate()
End If
To remove a comment from the document, use the CommentCollection.Remove method as shown in the code snippet below.
Document document = richEditControl.Document;
if (document.Comments.Count > 0)
{
//Remove the fourth document comment
document.Comments.Remove(document.Comments[3]);
}
Dim document As Document = richEditControl.Document
If document.Comments.Count > 0 Then
'Remove the fourth document comment
document.Comments.Remove(document.Comments(3))
End If
Users can use the Review Ribbon tab to insert, modify or remove comments. Refer to the How to: Create the RichEditControl with a Ribbon UI topic for more information on how to add the ribbon menu to the application.
The Comment group on the Review tab allows users to insert new comments, remove existing comments, and navigate through all the comments in the document.
The Tracking group buttons allow users to highlight comments in the document, to filter them by the author, and specify whether to display comments in the document margins or Reviewing Pane.
To add the Reviewing Pane to the RichEditControl, click the RichEditControl’s smart tag in the designer mode and select Create Reviewing Pane.
Tip
Set the RichEditControl’s DocumentCapabilitiesOptions.Comments property to Disabled to disable comments.
The comments are displayed in the Reviewing Pane or in balloons that appear in the document margins. The related ranges are highlighted by a different color for each reviewer.
Use the CommentOptions‘s class properties to change the comment displaying options, such as range highlighting color or comment visibility. Access these properties via the richEditControl.Options.Annotations.Comments notation.