Previous | Index | Next 

[PRB] SelFontName, SelFontSize, SelBold, SelItalic, SelUnderline, and SelStrikeThru properties of RichTextBox control return Nothing instead of Null

Under VB6, the SelFontName, SelFontSize, SelBold, SelItalic, SelUnderline, and SelStrikeThru properties of the RichTextBox control return Null if the selection contains characters with different attributes. For example, if the selection contains both regular characters and characters in boldface then the SelBold property returns Null.

Many word processor-like VB6 applications display a toolbar that allows users to change the attributes of the text that is currently selected in a RichTextBox control. The code that executes when a button on the toolbar is clicked is similar to the following one:

        Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
            Select Case Button.Key
                Case "Bold"
                    RichTextBox1.SelBold = Not RichTextBox1.SelBold
                Case "Italic"
                    RichTextBox1.SelItalic = Not RichTextBox1.SelItalic
                Case "Underline"
                    RichTextBox1.SelUnderline = Not RichTextBox1.SelUnderline
                ' more commands here ...
            End Select
        End Sub

Even if it isn’t immediately apparent, this code relies on the fact that the SelBold, SelItalic, and SelUnderline properties return Null if the selected characters have mixed attributes. In fact, if the SelBold property returns Null, then the Not operator converts such Null value to True, therefore characters in the selection get the boldface style.

The VB6RichTextBox control exposes all the SelXxxx properties that the original VB6 control exposes; if characters with mixed attributes are currently selected these properties return either False or Nothing. More precisely, they return False if all the characters in the selection use same font but possibly different attributes; they return Nothing if the selection includes characters rendered with a different font.

This behavior difference rarely affects converted applications. For example, the previous code continues to work correctly regardless of whether the SelBold, SelItalic, and SelUnderline properties return False or Nothing. In either case, the new value of the property will be True, exactly as it happens in the original VB6 code.

In some cases, however, this detail can be important, for example when the original VB6 code explicitly tests the value of these properties with the IsNull function. Keep it in mind when you see that a RichTextBox in the converted application doesn’t work as expected.


Previous | Index | Next