VB Migration Partner

KNOWLEDGE BASE - Forms and controls


Previous | Index | Next 

[PRB] UserControl classes don’t raise the InitProperties, ReadProperties and WriteProperties events

A VB6 UserControl fire three events that have no counterpart under VB.NET:

  • the InitProperties event fires only at design time, the very first time the control is dragged from the Toolbox onto the form’s surface;
  • the WriteProperties event fires only at design time; inside this event the control stores property values as set by the developer in the property window;
  • the ReadProperties event fires both at design time and runtime; in this event the control reads the properties stored in the .frm and .frx files.

None of these three events is supported by the VB6UserControl class. These events are superfluous, because Windows Forms use a persistence mechanism for properties that is based on code initialization –the code in the *.Designer.vb file – rather than on the PropertyBag object. For this reason, the lack of support for InitProperties, ReadProperties, and WriteProperties events is seldom an issue when converting a VB6 user control.

In some cases, however, at runtime the VB6 control relies on the ReadProperties event for tasks other than property deserialization. For example, VB6 developers may leverage the fact that this event fires after the user control has been hosted on the parent form. For example, consider the following simple user control that grabs a reference to the parent form so that it can resize itself when the parent form is resized:

        Dim WithEvents ParentForm As Form

        Private Sub ParentForm_Resize()
            Extender.Move 0, 0, Parent.ScaleWidth, Parent.ScaleHeight
        End Sub

        Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
            On Error Resume Next
            Set ParentForm = Parent
            ' more assignments here ...
        End Sub

In this particular case, the lack of support for the ReadProperties event prevents the user control from working correctly under VB.NET. The simplest workaround for this limitation is overriding the EndInit method in the .NET class and manually invoking the UserControl_ReadProperties method:

        '##InsertStatement Protected Overrides Sub EndInit()
        '##InsertStatement     MyBase.EndInit()
        '##InsertStatement     If Ambient.UserMode Then 
        '##InsertStatement         UserControl_ReadProperties(New VB6PropertyBag)
        '##InsertStatement     End If
        '##InsertStatement End Sub

Notice that this code passes an empty VB6PropertyBag object to the ReadProperties event handler; if code inside the handler uses the property bag’s ReadProperty method to assign one or more properties, such properties will be reset to their default value. This action would clearly cause incorrect behaviors later in program execution.

Another special case is when you dynamically load a UserControl at runtime, either using the Controls6.Add method or the Load method of a control array. In such cases, the VB6 runtime would fire an InitProperties event in the UserControl, whereas nothing happens in the converted .NET control. To avoid this minor difference you should invoke the special FireInitPropertiesEvent method immediately after loading the control

        Dim newCtrl As MyUserControl = Me.Controls6.Add("MyProject.MyUserControl", "newCtrl")
        newControl.FireInitPropertiesEvent() 
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