The text file format


A text file contains ASCII characters and control characters. The traditional text format for text files was to separate each line with a carriage return/line feed sequence (a CR/LF for short). If you download Unix or Macintosh text files, your lines might only be separated by a carriage return—or maybe a line feed. I can never remember, and I wish MS-DOS had followed this better, more efficient system. The CR/LF convention apparently comes from some old teletype standard where a carriage return would return to the start of the line without advancing to the next and a line feed would advance to the next line without returning to the beginning. You needed both to start the next line. In other words, this convention has little relevance to computer text files, and the Rich-TextBox control appears to ignore it. The control seems to display lines exactly the same whether they are separated by a carriage return, a line feed, or both.


The GUI age has brought us another definition of a text file. Rather than hard-coding line breaks with control characters, word processors generally string all the text of a paragraph together, leaving the application to set the margin and determine where to wrap lines. The simplest form of this syntax is to have each paragraph separated by a CR/LF. Notepad, for example, has a WordWrap setting on its Edit menu that toggles whether you want lines to wrap at Notepad’s margin or at the end of each line marked by a CR/LF. The RichTexBox control doesn’t have a direct way of turning off word wrap, but if you set a large enough right margin, it won’t be a problem. I provide a constant that does this for you:

edit.RightMargin = NoWordWrap

This works by setting the largest possible margin: 65,535. I also provide a Save-WordWrap property that uses the EM_FMTLINES message to tell the RichTextBox property to save what it calls “soft linebreaks” for text that is wrapped at the margin rather than at a CR/LF. A soft linebreak is two carriage returns and a line break. (Call it a CR/CR/LF.) I can’t imagine why you’d want such a thing, but RichEdit provides it, although RichTextBox, perhaps wisely, doesn’t pass it through.