Previous | Index | Next 

[PRB] The font of a control changes unexpectedly at runtime

VB6 and VB.NET greatly differ in the way they manage fonts in forms and controls. Under VB6 you can change the Font property of a form, a PictureBox, or another container control without affecting the font of child controls. In migrated VB.NET this isn’t true: any change in the Font property of a container affects the Font property of all child controls, unless you’ve specified a specific value for one or more font-related properties (FontName, FontSize, FontBold, and so forth).

In most circumstances this behavioral difference doesn’t affect the way migrated applications display their controls. If it does, however, you can easily work around this issue by invoking the FreezeControlsFont6 method for the container whose Font property will change during the form’s lifetime. You typically invoke this method in the handler for the Form_Load event:

        Private Sub Form_Load()
            '## InsertStatement FreezeControlsFont6(Me)
        End Sub

The FreezeControlsFont6 method (defined in the control support library) assigns a different Font object to each child control, so that child controls aren’t affected if a different value is later assigned to the container’s Font property.

In general you should pass the Me keyword (i.e. the reference to the current form or the current UserControl), so that the Font property of all child controls is affected. However, you can reduce the overhead of this method by specifying a container other than the form. For example, if your code modifies the Font property of a PictureBox and a Frame control (but not of the parent form), you can reduce the overhead by assigning only the Font property of the controls inside the PictureBox and Frame controls as follows:

        Private Sub Form_Load()
            '## InsertStatement FreezeControlsFont6(Picture1)
            '## InsertStatement FreezeControlsFont6(Frame1)
        End Sub

The FreezeControlsFont6 method is included in the VBMigrationPartner_Support module; if you add this file to the VB6 project you can avoid using the InsertStatement pragma and ensure that method and control names are spelled out correctly:

        ' this code assumes that VBMigrationPartner_SupportMethod module 
        ' is included in current project
        Private Sub Form_Load()
            FreezeControlsFont6 Me
        End Sub

 

Previous | Index | Next