How to: Create Bulleted List in Code
- 3 minutes to read
This example illustrates how to create a standard bulleted list in code.
- Add a new AbstractNumberingList instance to the Document.AbstractNumberingLists collection
- Specify a NumberingListBase.NumberingType property, so that it equals the NumberingFormat.Bullet value.
- For each list level, specify the paragraph characteristics using the ListLevel.ParagraphProperties property. Set the left indentation (the ParagraphPropertiesBase.LeftIndent property) so that each level has a different offset from the left. To specify a symbol that will be used to mark a list item, set the font via the ListLevel.CharacterProperties property and the character 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 bulleted 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.Bullet;
// Specify parameters for each list level.
ListLevel level = list.Levels[0];
CreateBulletedListHelper.AdjustLevelProperties(level, 100, 75, NumberingFormat.Decimal, new string('\u00B7', 1));
level = list.Levels[1];
CreateBulletedListHelper.AdjustLevelProperties(level, 300, 150, NumberingFormat.DecimalEnclosedParenthses, new string('\u006F', 1));
level = list.Levels[2];
CreateBulletedListHelper.AdjustLevelProperties(level, 450, 220, NumberingFormat.UpperRoman, new string('\u00B7', 1));
// 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 1\nLine 2\nLine 3");
// Convert paragraphs to list items.
document.BeginUpdate();
ParagraphCollection paragraphs = document.Paragraphs;
foreach (Paragraph pgf in paragraphs) {
pgf.ListIndex = 0;
pgf.ListLevel = 0;
}
paragraphs[1].ListLevel = 1;
paragraphs[2].ListLevel = 2;
document.EndUpdate();
class CreateBulletedListHelper {
public static void AdjustLevelProperties(ListLevel level, int leftIndent, int firstLineIndent, NumberingFormat format, string displayFormat) {
level.CharacterProperties.FontName = "Symbol";
level.ParagraphProperties.LeftIndent = leftIndent;
level.ParagraphProperties.FirstLineIndentType = ParagraphFirstLineIndent.Hanging;
level.ParagraphProperties.FirstLineIndent = firstLineIndent;
level.Start = 1;
level.NumberingFormat = format;
level.DisplayFormatString = displayFormat;
}
}