Back to Devexpress

CommentCollection.Add(CellRange, String, String) Method

officefileapi-devexpress-dot-spreadsheet-dot-commentcollection-dot-add-x28-devexpress-dot-spreadsheet-dot-cellrange-system-dot-string-system-dot-string-x29.md

latest8.2 KB
Original Source

CommentCollection.Add(CellRange, String, String) Method

Adds a note to the specified cell and returns the newly created object.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
Comment Add(
    CellRange range,
    string author,
    string text
)
vb
Function Add(
    range As CellRange,
    author As String,
    text As String
) As Comment

Parameters

NameTypeDescription
rangeCellRange

A CellRange object that specifies a cell to which a note is added. If you pass a range of cells, the note is added to the top-left cell in a range.

| | author | String |

A String value that specifies a name of the note author. This value is assigned to the Comment.Author property.

| | text | String |

A String value that specifies the text to be displayed in the note box. This value is assigned to the Comment.Text property.

|

Returns

TypeDescription
Comment

A Comment object that specifies the created note.

|

Example

This example demonstrates how to add a simple note (a legacy comment) to a cell and format the note text.

Create a Simple Note

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

Format Note Content

To apply different fonts to specific regions of the comment text, modify the CommentRunCollection collection returned by the Comment.Runs property. This collection stores the CommentRun objects that define regions of the comment text that are formatted specifically. After a comment has been created, its text is defined by a single run that is contained in the CommentRunCollection collection.

csharp
using DevExpress.Spreadsheet;
//...

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];

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

// Add a comment to the A1 cell.
Cell cell = worksheet.Cells["A1"];
Comment comment = worksheet.Comments.Add(cell, author, "This is important information for users.");

//Add the author name at the beginning of the comment.
CommentRunCollection runs = comment.Runs;
runs.Insert(0, author + ": \r\n");
runs[0].Font.Bold = true;

// Format the comment text.
runs[1].Font.Color = Color.Red;
runs[1].Font.Name = "Times New Roman";
runs[1].Font.Size = 14;
runs[1].Font.Italic = true;

// Add a new comment run. 
runs.Add("\n Never delete this comment!");
runs[2].Font.Color = Color.MidnightBlue;
vb
Imports DevExpress.Spreadsheet
'...

Dim workbook As New Workbook()
Dim worksheet As Worksheet = workbook.Worksheets(0)

'Get the system username. 
Dim author As String = workbook.CurrentAuthor

'Add a comment to the A1 cell.
Dim cell As Cell = worksheet.Cells("A1")
Dim comment As Comment = worksheet.Comments.Add(cell, author, "This is important information for users.")

'Add the author name at the beginning of the comment.
Dim runs As CommentRunCollection = comment.Runs
runs.Insert(0, author & ": " & Constants.vbCrLf)
runs(0).Font.Bold = True

'Format the comment text.
runs(1).Font.Color = Color.Red
runs(1).Font.Name = "Times New Roman"
runs(1).Font.Size = 14
runs(1).Font.Italic = True

'Add a new comment run. 
runs.Add(Constants.vbLf & "Never delete this comment!")
runs(2).Font.Color = Color.MidnightBlue

Remove Notes

Use the following methods to remove simple notes:

The following code snippets (auto-collected from DevExpress Examples) contain references to the Add(CellRange, String, String) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-spreadsheetcontrol-api-part-1/CS/SpreadsheetControl_WPF_API/SpreadsheetActions/CellActions.cs#L441

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

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/CellActions.vb#L214

vb
worksheet.Hyperlinks.Add(worksheet("D5"), "http://www.devexpress.com/", True, "DevExpress")
worksheet.Comments.Add(worksheet("B6"), "Me", "Cell Comment")
worksheet.Comments.Add(worksheet("C6"), "Me", "Cell Comment")

winforms-spreadsheetcontrol-api-part1/VB/SpreadsheetControl/SpreadsheetActions/CellActions.vb#L437

vb
Dim commentedCell As Cell = worksheet.Cells("A2")
Dim commentA2 As Comment = worksheet.Comments.Add(commentedCell, author, "This is a comment")
commentA2.Visible = True

wpf-spreadsheetcontrol-api-part-1/VB/SpreadsheetControl_WPF_API/SpreadsheetActions/CellActions.vb#L396

vb
Dim commentedCell As Cell = worksheet.Cells("A2")
Dim commentA2 As Comment = worksheet.Comments.Add(commentedCell, author, "This is a comment")
commentA2.Visible = True

See Also

CommentCollection Interface

CommentCollection Members

DevExpress.Spreadsheet Namespace