Previous | Index | Next 

[PRB] A reference to the Controls collection isn’t converted to Controls6

Both VB6 and VB.NET forms expose the Controls collection; however, this collection behaves quite differently in the two languages. More precisely, the VB6 Controls collection includes all the controls on the form (including controls contained in child controls, such as a PictureBox) and it even contains nonvisible components such as Timer and CommondDialog controls. Conversely, the .NET Controls collection includes only “real” controls – that is, objects that derive from the System.Windows.Forms.Control class – and only controls that sit directly on the form’s or the usercontrol’s surface.

For this reason, VB Migration Partner automatically converts references to the Controls collection to the Controls6 collection, a property exposed by the VB6Form and VB6UserControl objects. For example, the following VB6 code:

        n = Me.Controls.Count

translates to VB.NET as:

        n = Me.Controls6.Count

However, VB Migration Partner can’t make the substitution when the form is referenced via late-binding, as in the following case:

        Sub Test(ByVal ctrl As Control)
            n = ctrl.Parent.Controls.Count ' Parent property returns an Object 
        End Sub

In such cases you must remedy by means of one of the following techniques.

The most obvious remedy is, of course, by manually morphing the Controls reference into Controls6 after the migration step, but this technique doesn’t support the Convert-Test-Fix and isn’t a wise choice if you plan to migrate the same application multiple times.

A smarter solution is to modify the VB6 code so that the form isn’t referenced via late binding, as in this code:

        Dim frm As Form: Set frm = ctrl.Parent
        n = frm.Controls.Count

Another possible solution is based on the GetControlsCollection method defined in the VBMigrationPartner_Support module:

        n = GetControlsCollection(ctrl.Parent)

If you consistently reference the Controls collection through the Parent property of a control, you might try the following PostProcess pragma:

        '## project:PostProcess "\.Parent\.Controls", "${0}6"

This approach doesn’t require that you modify the executable VB6 code, but you should at least ensure that the regular expression doesn’t have any false match.


Previous | Index | Next