How to: Create Numbered List in Code
- 3 minutes to read
To create a numbered list in code, perform the following steps.
- Add a new AbstractNumberingList instance to the Document.AbstractNumberingLists collection.
- Specify a NumberingListBase.NumberingType property, so that it is equal to the NumberingType.MultiLevel value.
- For each list level, specify the paragraph characteristics using the ListLevel.ParagraphProperties property. Set the left indentation so that each level has a different offset from the left. Specify a hanging first line and set the first line indent value to provide enough space for a number that precedes the text.
- Set the ListLevelProperties.Start value that is the number from which to start the list.
- Specify a format used to display a number via the ListLevelProperties.DisplayFormatString property.
- Use the Add method of the collection of lists in the document (accessible via the Document.NumberingLists property) to add a list definition to the document. The parameter is the index of an abstract numbering list created previously. The Add method creates a pattern that can be applied to a paragraph so that the paragraph looks like a list item.
- To include a paragraph in a list, set the Paragraph.ListIndex property of a paragraph to the index of a list in the document and specify the Paragraph.ListLevel property.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219
document.BeginUpdate();
// Define an abstract list that is the pattern for lists used in the document.
AbstractNumberingList list = document.AbstractNumberingLists.Add();
list.NumberingType = NumberingType.MultiLevel;
// Specify parameters for each list level.
ListLevel level = list.Levels[0];
CreateNumberedListHelper.AdjustLevelProperties(level, 150, 75, NumberingFormat.Decimal, "{0}");
level = list.Levels[1];
CreateNumberedListHelper.AdjustLevelProperties(level, 300, 150, NumberingFormat.DecimalEnclosedParenthses, "{0}→{1}");
level = list.Levels[2];
CreateNumberedListHelper.AdjustLevelProperties(level, 450, 220, NumberingFormat.UpperRoman, "{0}→{1}→{2}");
// Create a list for use in the document. It is based on a previously defined abstract list with ID = 0.
document.NumberingLists.Add(0);
document.EndUpdate();
document.AppendText("Line one\nLine two\nLine three\nLine four");
// Convert all paragraphs to list items of level 0.
document.BeginUpdate();
ParagraphCollection paragraphs = document.Paragraphs;
foreach (Paragraph pgf in paragraphs)
{
pgf.ListIndex = 0;
pgf.ListLevel = 0;
}
// Specify a different level for a certain paragraph.
paragraphs[1].ListLevel = 1;
document.EndUpdate();
class CreateNumberedListHelper {
public static void AdjustLevelProperties(ListLevel level, int leftIndent, int firstLineIndent, NumberingFormat format, string displayFormat) {
level.ParagraphProperties.LeftIndent = leftIndent;
level.ParagraphProperties.FirstLineIndentType = ParagraphFirstLineIndent.Hanging;
level.ParagraphProperties.FirstLineIndent = firstLineIndent;
level.Start = 1;
level.NumberingFormat = format;
level.DisplayFormatString = displayFormat;
}
}