Text and Rich Text Modes


The RichTextBox control is chock full of cool features that often get in your way and cause headaches. Sometimes multiple fonts and formatted text are overkill. Sometimes you just want a souped-up Notepad.


Fortunately, the RichEdit Windows control has EM_GETTEXTMODE and EM-_SETTEXTMODE messages. Unfortunately, these messages are undocumented and appear to have absolutely no effect on any of the syntax variations I could think of. Well, that’s how it goes with undocumented features. I’ll have to hack out my own version by maintaining an internal fTextMode variable and disabling all the rich text features when it is True.


The implementation of the TextMode property is the classic Get/Let pair that simply returns or modifies the fTextMode variable.

Property Get Textmode() As Boolean
TextMode = fTextMode
End Property

Property Let TextMode(ByVal fTextModeA As Boolean)
' Change to TextMode dirties the file, but not vice versa
If Not fTextMode and fTextMode <> fTextModeA Then DirtyBit = True
fTextMode = fTextModeA
PropertyChanged "TextMode"
End Property

The interesting part is what you do with the TextMode properties in XEditor properties and methods. For example, here’s what happens in the SaveFile method:

Sub SaveFile(sFileNameA As String, _
Optional ordTextModeA As ELoadSave = elsDefault)
If sFileNameA = sEmpty Then Exit Sub
BugAssert ordTextModeA >= elseDefault And ordTextModeA <= elstext
If ordTextModeA = elsDefault Then
ordTextModeA = IIf(TextMode, elsText, elsRTF)
End If
' Use RichTextBox method (raise unhandled errors to caller)
sFileNameA = GetFullPath(sFileNameA)
txt.SaveFile sFileNameA, ordTextModeA
sFilePath = sFileNameA
DirtyBit = False
End Sub

The LoadFile method shown earlier works much the same. In both cases, the work is delegated to the appropriate method (LoadFile or SaveFile) of the
internal RichTextBox control. This method takes an argument that specifies whether to save the Text property or the TextRTF property. Most of the code in SaveFile and LoadFile deals with figuring out the appropriate mode to save in.


When I started working with the RichTextBox, I had some preconceptions about how LoadFile and SaveFile should work. This might be obvious to everyone else, but bear with me while I explain a little text mode history.