Back to Devexpress

CellRange.SetRichText(RichTextString) Method

officefileapi-devexpress-dot-spreadsheet-dot-cellrange-dot-setrichtext-x28-devexpress-dot-spreadsheet-dot-richtextstring-x29.md

latest6.4 KB
Original Source

CellRange.SetRichText(RichTextString) Method

Assigns the rich formatted text to a cell.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
void SetRichText(
    RichTextString value
)
vb
Sub SetRichText(
    value As RichTextString
)

Parameters

NameTypeDescription
valueRichTextString

A RichTextString object that is a combination of specifically formatted text regions.

|

Remarks

Use the SetRichText method to add rich formatted text to a cell. Such rich text is defined by the RichTextString object and represents a cell text divided into separate regions (so-called text runs), each with its own set of font characteristics.

To apply rich formatting to the cell text, do one of the following:

Pass the RichTextString instance to the SetRichText method to assign the specified rich text to a cell. The resulting text string is also set to the cell’s CellRange.Value as plain text.

csharp
// Create a RichTextString instance.
RichTextString richText = new RichTextString();

// Add three text runs. Each run has its own font settings.
richText.AddTextRun("Rich ", new RichTextRunFont("Arial", 14, System.Drawing.Color.FromArgb(0xc5, 0x9f, 0xc9)));
richText.AddTextRun("text ", new RichTextRunFont("Tahoma", 14, System.Drawing.Color.FromArgb(0x2c, 0x60, 0x8e)));
richText.AddTextRun("formatting", new RichTextRunFont("Castellar", 14, System.Drawing.Color.FromArgb(0x2f, 0x24, 0x4f)));

// Assign the rich formatted text to the cell B2.
worksheet["B2"].SetRichText(richText);
vb
' Create a RichTextString instance.
Dim richText As New RichTextString()

' Add three text runs. Each run has its own font settings.
richText.AddTextRun("Rich ", New RichTextRunFont("Arial", 14, System.Drawing.Color.FromArgb(&Hc5, &H9f, &Hc9)))
richText.AddTextRun("text ", New RichTextRunFont("Tahoma", 14, System.Drawing.Color.FromArgb(&H2c, &H60, &H8e)))
richText.AddTextRun("formatting", New RichTextRunFont("Castellar", 14, System.Drawing.Color.FromArgb(&H2f, &H24, &H4f)))

' Assign the rich formatted text to the cell B2.
worksheet("B2").SetRichText(richText)

For more examples on how to apply different fonts to specific regions of the cell text, refer to the How to: Apply Rich Formatting to Cell Text document.

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the SetRichText(RichTextString) 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.

winforms-spreadsheet-how-to-edit-rich-text/CS/SpreadsheetRichText/Form1.cs#L21

csharp
richText.AddTextRun("formatting", new RichTextRunFont("Castellar", 14, System.Drawing.Color.FromArgb(0x2f, 0x24, 0x4f)));
worksheet["B2"].SetRichText(richText);
worksheet["B5"].SetValueFromText("This is the plain text\nUse the context menu to set rich text formatting");

winforms-spreadsheet-how-to-edit-rich-text/VB/SpreadsheetRichText/Form1.vb#L15

vb
richText.AddTextRun("formatting", New RichTextRunFont("Castellar", 14, System.Drawing.Color.FromArgb(&H2F, &H24, &H4F)))
worksheet("B2").SetRichText(richText)
worksheet("B5").SetValueFromText("This is the plain text" & vbLf & "Use the context menu to set rich text formatting")

See Also

AddTextRun(String, RichTextRunFont)

Characters

How to: Apply Rich Formatting to Cell Text

CellRange Interface

CellRange Members

DevExpress.Spreadsheet Namespace