How to: Add and Modify Fields
- 3 minutes to read
The following example illustrates how to add and modify fields manually.
All the fields in the document are contained in the FieldCollection, accessible through the SubDocument.Fields property.
Insert a Field
To create a field, use the FieldCollection.Create property, as illustrated below.
This code snippet inserts the DATE field at the caret position.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219
DocumentPosition caretPosition = document.CaretPosition;
//Start updating the document
caretPosition.BeginUpdateDocument();
//Create a DATE field at the caret position
document.Fields.Create(caretPosition, "DATE");
document.Fields.Update();
//Finalize the modification
document.EndUpdate();
To create a field from the selected range, use the FieldCollection.Create property with the passed range.
This code snippet creates a field from the selected range so that the selected text becomes the field code.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219
document.BeginUpdate();
//Insert the text to the document end
document.AppendText("SYMBOL 0x54 \\f Wingdings \\s 24");
document.EndUpdate();
//Convert the inserted text to the field
document.Fields.Create(document.Paragraphs[0].Range);
document.Fields.Update();
Modify Field Code
To modify field content, use the SubDocument.BeginUpdate - SubDocument.EndUpdate paired methods.
To insert a field format switch, use the SubDocument.InsertText method. Use the end position of the field code range as a method argument.
Use the Field.Update method to update the field result.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219
DocumentPosition caretPosition = document.CaretPosition;
SubDocument currentDocument = caretPosition.BeginUpdateDocument();
//Create a DATE field at the caret position
currentDocument.Fields.Create(caretPosition, "DATE");
currentDocument.EndUpdate();
for (int i = 0; i < currentDocument.Fields.Count; i++)
{
string fieldCode = document.GetText(currentDocument.Fields[i].CodeRange);
if (fieldCode == "DATE")
{
//Retrieve the range obtained by the field code
DocumentPosition position = currentDocument.Fields[i].CodeRange.End;
//Insert the format switch to the end of the field code range
currentDocument.InsertText(position, @"\@ ""M/d/yyyy h:mm am/pm""");
}
}
//Update all document fields
currentDocument.Fields.Update();
Note
The header and footer as well as the text box in the RichEditControl document do not belong to the main document body. Use the Section.BeginUpdateHeader - Section.EndUpdateHeader or Section.BeginUpdateFooter - Section.EndUpdateFooter paired methods to obtain the header or footer. Refer to the How to: Create and Modify Header topic for details. The TextBox.Document property allows you to retrieve the text box content and update the corresponding fields.