Previous | Index | Next 

[PRB] Label and Image controls appear in front of other controls

VB6 Label and Image controls are lightweight controls, which means that they don’t actually correspond to actual Windows controls. Instead, the corresponding label or image are just “drawn” over the parent form’s surface (or more in general, the surface of the container control). For this reason, a VB6 Label or Image control can never appear in front of other non-lightweight controls.

Another way for explaining this fact is that VB6 manages two distinct ZOrder planes: one for standard controls and one for lightweight controls: a control in the lightweight ZOrder plan can never appear in front of any control in the main ZOrder plane. If two or more lightweight controls overlap, their TabIndex property dictates which control appears on top of the others.

The .NET Framework manages neither lightweight controls nor multiple ZOrder planes, therefore all VB6Label and VB6Image control may actually appear in front of other controls. If this is the case, you can solve the problem in many ways:

  1. You use the BringToFront or SendToBack pragmas to precisely control the ZOrder of all the controls in a given form
  2. Manually fix the visibility of migrated controls inside the Visual Studio designer, save the resulting *.Designer.vb file in a safe location on disk, and then use the PostCommand pragma to force VB Migration Partner to use this modified .NET form at each subsequent migration.
  3. Set the VB6Form.ArrangeLightweightControls property to true, to automatically send lightweight controls “behind” regular controls in all forms of the migrated application.
  4. Use the VB6Utils.ArrangeLightweightControls method to fix the lightweight controls’ ZOrder for a specific form.

Starting with VB Migration Partner version 1.32 you can use VB6Form.ArrangeLightweightControls static property to force migrated .NET applications to behave exactly as in VB6, without any further manual intervention on your part. All you need to do is setting this property to True before loading the first form in the application:

        Sub Main()
            VB6Form.ArrangeLightweightControls = True
        End Sub

The initial (default) value of the ArrangeLightweightControls property is False, because rearranging the controls when the form starts is a time-consuming operation – especially with forms that contains hundreds controls. If you are concerned about loading speed, manually fixing the ZOrder of individual controls – as described by options a) and b) – is the recommended solution.

The VB6Form.ArrangeLightweightControls property suffers from two minor limitations: it isn’t granular (it slows down all forms in the application, including those that don’t strictly require it) and doesn’t affect controls that are added dynamically via code after the form has been loaded. You can work around these limitations by manually invoking VB6Utils.ArrangeLightweightControls method, as in this example:

        ' dynamically add a label control
        Dim newLabel As VB6Label = Me.Controls6.Add("VB.Label", "newLabel")
        ' ensure the new label goes behind all standard controls
        ' make it visible
        newLabel.Visible = True


Previous | Index | Next