Skip to main content

How to: Create, Edit and Copy Simple Notes in Spreadsheet for WinForms

  • 5 minutes to read

This example demonstrates how to create a new simple note (legacy comment), edit its text and copy the existing note to another cell.

Create a Simple Note

To create a new note and associate it with a cell, retrieve the worksheet’s collection of cell comments from the Worksheet.Comments property and call the CommentCollection.Add method.

To make the added note visible in the cell, set the Comment.Visible property to true.

Edit Note Text

To edit the simple note text, modify the CommentRunCollection collection, which is obtained from the Comment.Runs property. This collection stores the CommentRun objects that define separate regions of the note text. After a note is created, its text is defined by a single run that is contained in the CommentRunCollection collection. You can add more runs to the notes, edit the text of the existing runs or completely remove them from the note.

Note

The SpreadsheetControl does not support rich text formatting for comments. You can, however, use comment runs to add new information to a comment or change the existing comment text.

Copy Simple Notes

To copy the existing note to another cell, call the CellRange.CopyFrom method of the Cell object corresponding to the cell into which you wish to insert the copied note. Pass the source cell and the PasteSpecial.Comments enumerator value to insert only the note and ignore cell content. The copied note replaces any existing note or comment in the destination area.

Note that the copied note preserves the structure of the source note and contains the same number of runs. To edit the text of the copied note, call the CommentCollection.GetComments method to retrieve the inserted note, and then modify the CommentRun.Text property of the second run.

Remove Simple Notes

To remove simple from cells, use the CommentCollection.Remove, CommentCollection.RemoveAt, CommentCollection.Clear or Worksheet.ClearComments methods.

Example: Insert, Edit, and Copy a Simple Note

The code sample below creates a new simple note, adds an author’s name at the note start, and copies the note to another cell.

SpreadsheetControl_AddCommentExample

View Example

// Get the system username. 
string author = workbook.CurrentAuthor;

// Add a comment to the "A2" cell.
Cell commentedCell = worksheet.Cells["A2"];
Comment commentA2 = worksheet.Comments.Add(commentedCell, author, "This is a comment");
commentA2.Visible = true;

// Insert the author's name at the beginning of the comment.
CommentRunCollection commentRunsA2 = commentA2.Runs;
commentRunsA2.Insert(0, author + ": \r\n");

// Copy the comment to the "E2" cell.
worksheet.Cells["E2"].CopyFrom(commentedCell, PasteSpecial.Comments);

// Get the added comment and make it visible.
Comment commentE2 = worksheet.Comments.GetComments(worksheet["E2"])[0];
commentE2.Visible = true;

// Modify text of the copied comment.
CommentRunCollection commentRunsE2 = commentE2.Runs;
commentRunsE2[1].Text = "This comment is copied from the cell " + commentedCell.GetReferenceA1();

Manage Simple Notes in the User Interface

Users can insert, edit, hide or delete simple notes by Ribbon UI (by the commands located in the Comments group of the Review tab) or context menu.

Spreadsheet_CommentsRibbonGroup

Spreadsheet_CommentContextMenu

Use the sizing handles (small rectangles at the corners and sides of the comment box) visible for a selected comment to resize a simple note or move it to a new location.

Spreadsheet_ResizeComment

Tip

A cell with a simple note has a red triangular indicator in the corner. Handle the CustomDrawCommentIndicator event to customize an indicator’s appearance.

Restrict Simple Note Operations

To restrict end-user actions over simple notes, use the SpreadsheetBehaviorOptions.Comment property, which obtains restriction settings. The table below lists possible restrictions you can set to prevent modifications of simple notes in the SpreadsheetControl UI.

Restriction Description
Insert Gets or sets whether a user can create new simple note.
Edit Gets or sets whether a user can edit the existing simple note.
Delete Gets or sets whether a user can delete simple notes.
ShowHide Gets or sets whether a user can display or hide simple notes.
ShowOnHover Gets or sets whether to show simple notes when a user hovers over a cell with a comment indicator.
Resize Gets or sets whether a user can change the size of the note box.
Move Gets or sets whether a user can move notes to a new location.

Set the required property to DocumentCapability.Disabled or DocumentCapability.Hidden to disable or hide the corresponding command in the ribbon UI and the context menu.

Manage Threaded Comments

The SpreadsheetControl for WinForms allows you to manage threade comments in code. Refer to the following article for more information: Threaded Comments in Spreadsheet for WinForms

See Also