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