VB Migration Partner

KNOWLEDGE BASE - Forms and controls


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 




Follow Francesco Balena on VB6 migration’s group on

LinkedIn





Read Microsoft Corp’s official case study of a VB6 conversion using VB Migration Partner.




Code Architects and its partners offers remote and onsite migration services.

More details




Subscribe to our free newsletter for useful VB6 migration tips and techniques.

newsletter



To learn more about your VB6 applications, run VB6 Analyzer on your source code and send us the generated text file. You will receive a detailed report about your VB6 applications and how VB Migration Partner can help you to quickly and effectively migrate it to .NET.

Get free advice



A fully-working, time-limited Trial Edition of VB Migration Partner allows you to test it against your actual code

Get the Trial




The price of VB Migration Partner depends on the size of the VB6 application, the type of license, and other factors

Request a quote




Migrating a VB6 application in 10 easy steps

Comparing VB Migration Partner with Upgrade Wizard

Migration tools: Feature Comparison Table

All whitepapers