Using the Rich Textbox Control
The Rich Textbox control allows you to design a text box for writing code that affects the selected text. You can specify the Font, Font Size, Bold, and Italic properties. You can even add bullet points to the selected text. Furthermore, you can save the contents of the Rich Textbox control in a rich text format (RTF) file and later bring it back into the control.The following code illustrates using several of the Rich Textbox control's properties:Private Sub cmdTextColor_Click()
'Show the Color dialog
dlgCommon.ShowColor
'Change the color of the selected text
'to the color selected in the dialog
rtfDocument.SelColor = dlgCommon.Color
End Sub
This code uses the Color common dialog box to set the SelColor property of the Rich Textbox control. The selected text appears in whatever color the user selects from the common dialog box.The Click event of the cmdTextFont command button sets the SelFontName, SelBold, SelItalic, and SelFontSize properties of the Rich Textbox control to the font, style, and size selected in the Font common dialog box:Private Sub cmdTextFont_Click()
'Set Flags to only show screen fonts
dlgCommon.Flags = cdlCFScreenFonts
'Display the Font dialog
dlgCommon.ShowFont
'Change the font of the selected text to the
'font settings designated in the Font dialog
With rtfDocument
.SelFontName = dlgCommon.FontName
.SelBold = dlgCommon.FontBold
.SelItalic = dlgCommon.FontItalic
.SelFontSize = dlgCommon.FontSize
End With
End Sub
The selected attributes are applied only to the selected text.The Rich Textbox control has a method called SaveFile that lets you save the contents of the Rich Textbox control to an RTF file. The code looks like this:Private Sub cmdSave_Click()
'Designate default settings for the Save dialog
dlgCommon.Filter = "RTF Files (*.rtf)|*.rtf"
'Invoke the Save dialog
dlgCommon.ShowSave
'If no file is selected, display an error message
'If a file is selected, save the file with the name and
'location designated in the dialog
If dlgCommon.FileName = " Then
MsgBox "You Must Specify a File Name", vbExclamation, "File NOT Saved!"
Else
rtfDocument.SaveFile dlgCommon.FileName
End If
End Sub
The code begins by setting the Common Dialog control's Filter property; this filters the filenames displayed in the File Save common dialog box. The ShowSave method invokes the Save As common dialog box (see Figure 21.18). After the user types in or selects a filename, the Common Dialog control's FileName property is filled in with the name of the file that the user specified. If the user clicks Cancel, the FileName property contains a zero-length string, and the user is warned that the file wasn't saved.
Figure 21.18. The Save As common dialog box allows you to enter a name, a location, and an extension to your file.

As mentioned, you can also retrieve the contents of an RTF file into the control. The code looks like this:Private Sub cmdOpen_Click()
'Set initial values for the Open dialog
dlgCommon.FileName = "
dlgCommon.Filter = "RTF Files (*.rtf)|*.rtf"
dlgCommon.InitDir = CurDir
'Display the dialog
dlgCommon.ShowOpen
'If the user did not select a file, display a message
'If the user selected a file, load the file they selected
If dlgCommon.FileName = " Then
MsgBox "You Must Specify a File Name", vbExclamation, "File Cannot Be Opened!"
Else
rtfDocument.LoadFile dlgCommon.FileName
End If
End Sub
The Click event of the cmdOpen command button uses the ShowOpen method to invoke the File Open common dialog box (see Figure 21.19). If the user selects a file, the Rich Textbox control's LoadFile method uses the Common Dialog control's FileName property as the name of the file to open.
Figure 21.19. The Open common dialog box not only lets you specify which files to open, but also allows you to navigate around your computer and network.

Besides being able to open and save the contents of a Rich Textbox control, you can print the control's contents. The Click event of the cmdPrint command button sets the Common Dialog control's Flags property to cdlPDAllPages:Private Sub cmdPrint_Click()
'Select All in the Print dialog
dlgCommon.Flags = cdlPDAllPages
'Show the Print dialog
dlgCommon.ShowPrinter
'Print the selected text with the settings
'selected in the Print dialog
rtfDocument.SelPrint dlgCommon.hDC
End Sub
This selects the All option button in the Print dialog box (and deselects the Pages and Selection option buttons). The ShowPrinter method displays the Print common dialog box (see Figure 21.20). The SelPrint method of the Rich Textbox control is then used to print the selected text with the printer selected in the Print common dialog box.
Figure 21.20. The Print common dialog box has several options for printing chores.
