Better user experience and productivity often result from simplicity and automation. You can programmatically convert plain text lists into actual bulleted lists using the powerful API of TX Text Control. This feature not only improves the appearance of documents, but also makes them more structured and easier to read. Let's explore why this approach is so valuable and how it works with a code example.

During document creation, users often create content in plain text editors or import text from external sources. Plain text lists-those that are preceded by characters such as dashes - or asterisks * are a common way to organize information. However, these lists lack the proper formatting and structure of true bulleted lists. Once converted, lists can be easily modified using TX Text Control features. For example, you can change bullet styles or adjust indentation levels.

Converting Plain Text Lists

TX Text Control provides a robust API that allows developers to recognize and convert plain text lists into structured bulleted lists. This includes text lines that begin with common list characters such as -, *, or +. Indented lists created with tabs are also supported, allowing seamless conversion of hierarchical list structures.

The following method converts the currently selected text in a TX Text Control document into a bulleted list.

void ApplyBulletedListFormatting(TextControl textControl, int baseLeftMargin)
{
// Define the set of valid bullet characters
var bulletChars = new HashSet<char> { '?', 'o', 'O', '0', '-', '?', '?', '*', '+' };
TXTextControl.Selection selection = textControl.Selection;
int offset = 0;
// Iterate through all paragraphs in the TextControl
foreach (Paragraph paragraph in textControl1.Paragraphs)
{
// Check if the paragraph is within the selection
if (paragraph.Start < selection.Start - offset || paragraph.Start > selection.Start + selection.Length - offset)
continue;
string text = paragraph.Text;
int textLength = text.Length;
if (textLength > 1)
{
int tabCount = 0;
// Count the number of leading tab characters
while (tabCount < textLength && text[tabCount] == '\t')
{
tabCount++;
}
// Check if there's a bullet character followed by a space
if (tabCount < textLength - 1 &&
bulletChars.Contains(text[tabCount]) &&
text[tabCount + 1] == ' ')
{
textControl1.Selection.Length = textControl1.Selection.Length - 1;
// Set list type to bulleted
paragraph.ListFormat.Type = ListType.Bulleted;
// Remove the leading tabs and bullet character
textControl1.Select(paragraph.Start - 1, tabCount + 2);
textControl1.Selection.Text = string.Empty;
// Adjust the selection offset
offset += tabCount + 2;
// Configure list level and indentation
paragraph.ListFormat.Level = tabCount + 1;
paragraph.ListFormat.LeftIndent = tabCount > 0 ? baseLeftMargin * tabCount : 0;
}
}
}
}
view raw test.cs hosted with ❤ by GitHub

Step-by-Step Process

TX Text Control is used to loop through the paragraphs in the selected area of text. Then it checks the depth of the list by counting the number of tabs that follow, and checking to see if there is a leading bullet followed by a space.

- First item
- Second item
- Sub-item 1
- Sub-item 2
view raw test.txt hosted with ❤ by GitHub

The paragraph is then converted to a bulleted list with the appropriate level and indent, and the leading text characters are removed.

Bulleted list conversion

Conclusion

Converting plain text lists to bulleted lists is a powerful feature that improves the appearance and structure of documents. This approach is especially useful when importing content from external sources or when users create content in plain text editors. By automating the conversion process, you can improve the user experience and productivity of your applications.