Back to Devexpress

Comments in Rich Text Documents

windowsforms-18077-controls-and-libraries-rich-text-editor-rich-edit-control-document-comments.md

latest12.5 KB
Original Source

Comments in Rich Text Documents

  • Jul 01, 2025
  • 6 minutes to read

The RichEditControl allows you to add comments to documents. You can add, edit, and remove comments in code and from the UI.

Create Comments

Use the following API to attach a comment to a document range:

MemberDescription
Paragraph.RangeRetrieves the paragraph’s range.
SubDocument.FindAllFinds all document ranges that contain the expression or text string.
SubDocument.CommentsProvides access to the document’s Comment collection.
CommentCollection.CreateCreates 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”.

csharp
// 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));
}
vb
' 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

Create Nested Comments

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.

csharp
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);
}
vb
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

Edit Comments

Edit Comment Content

Use the API from the table below to edit the comment’s content.

MemberDescription
Comment.BeginUpdateOpens the comment for modification. Use the SubDocument object returned by the BeginUpdate method to add and edit comment content.
SubDocument.InsertTextInserts a text string into the specified position.
TableCollection.CreateInserts a table into a document position.
ShapeCollection.InsertPictureInserts a floating picture into the specified position position.
DocumentImageCollection.InsertInserts an inline image in the specified position.
ShapeCollection.InsertTextBoxInserts a text box in the specified position.
Comment.EndUpdateFinalizes the comment update.

The following code sample adds text to the empty comment:

csharp
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);
    }
}
vb
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

Edit Comment Attributes

Use the following members to change the comment parameters (author, date, etc.):

MemberDescription
Comment.BeginUpdateOpens the comment for editing.
Comment.AuthorSpecifies the comment’s author.
Comment.DateSpecifies the comment’s last modified date.
Comment.NameSpecifies the comment’s name.
Comment.EndUpdateFinalizes the comment update.

The code snippet below changes the comment’s name, date and author.

csharp
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();
}
vb
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

Delete Comments

To remove a comment from the document, use the CommentCollection.Remove method as shown in the code snippet below.

csharp
Document document = richEditControl.Document;
if (document.Comments.Count > 0)
  {
   //Remove the fourth document comment
   document.Comments.Remove(document.Comments[3]);
  }
vb
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

Comments in the User Interface

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.

Display Options

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.