In the last blog entry, we introduced the TextPartCollection to modify content in all text parts of a document. This sample shows a typical scenario using this meta collection as this list type handling should work in all text parts including headers, footers and the main text.

When creating a numbered or bulleted list in TX Text Control, the following paragraph will inherit this formatting and the list will be continued in the next paragraph.

Windows Forms and WPF: End a list on return when line is empty

In some cases, you might want to end the list for example, if the last line is empty:

Windows Forms and WPF: End a list on return when line is empty

The following code is trapping the KeyPress event in order to check for the Enter key. In case, the last line is empty, the list format type is removed:

private void textControl1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (Convert.ToInt32(e.KeyChar))
{
// Enter key is pressed
case 13:
// get the currently active TextPart
IFormattedText textPart =
(IFormattedText)textControl1.TextParts.GetItem();
// get the current line
TXTextControl.Line line =
textPart.Lines.GetItem(textPart.Selection.Start);
// remove the numbered list format type, if the line is empty
if (line.Text == "" || line.Text == "\r\n")
{
if (textPart.Selection.ListFormat.Type !=
TXTextControl.ListType.None)
{
textPart.Selection.ListFormat.Type =
TXTextControl.ListType.None;
}
}
break;
}
}
view raw gistfile1.cs hosted with ❤ by GitHub