Previous | Index | Next 

[PRB] Private UserControls aren’t loaded with Controls.Add method

If a UserControl class has a Friend scope and isn’t visible outside the current project, it can’t be loaded by a Controls.Add method, even if this method is called from inside the current project.

The explanation: the Controls.Add (which is translated as a reference to the Controls6.Add method) requires that the UserControl be public and be flagged with a VB6Object attribute. However, VB Migration Partner doesn’t automatically add this attribute when converting a non-public UserControl.

For example, let’s suppose the current project TestProject contains a private UserControl class named AddressControl. VB Migration Partner generates the following code for this class:

        Friend Class AddressControl
            …
        End Class

Any reference to this control inside the current project is migrated correctly. For example, if a form contains one or more occurrences of the AddressControl, these occurrences will be correctly migrated to VB.NET. However, if a form attempts to dynamically create an instance of the control by means of a Controls.Add method, the VB.NET code will throw an exception at runtime:

        Controls6.Add("AddressControl", "Address1")

To avoid this problem you have to change the class’s visibility to Public and insert a property VB6Object attribute:

        <VB6Object("TestProject.AddressControl")>  _
        Public Class AddressControl
            …
        End Class

The simplest way to do it is by means of a PostProcess pragma at the top of the AddressControl.ctl file:

        '## PostProcess "Friend Class AddressControl",
           "<VB6Object(""TestProject.AddressControl"")> Public Class AddressControl"

 

Previous | Index | Next