Previous | Index | Next 

[HOWTO] Write an extender DLL that can manipulate design-time properties of converted controls

VB Migration Partner can be extended by means of extender DLLs, which can be written in any .NET language, including VB.NET or C#. This article shows how to write an extender that is capable to change the design-time value of controls’ properties by applying transformations that can’t be obtained by any pragma (including the ChangeProperty pragma).

First, launch Visual Studio and create a new Class Library project in your favorite .NET language. In this example we will use VB.NET.

Next, switch the References tab page of the MyProject node and add a reference to the following two VB Migration Partner DLLs: CodeArchitects.VBMigrationEngine.dll and CodeArchitects.VBLibrary.dll.

In the General tab page of the MyProject node assign a name to your extender DLL. In this case we will use the name PropertyProcessorExtender.

Rename the Class1.vb file appropriately – for example, PropertyProcessor.vb – and replace its contents with the following code:

  Imports CodeArchitects.VB6Library
  '  this attribute tells VB Migration Partner that this DLL contains one or 
  '  more extenders
  <Assembly: VB6SupportLibrary(True)>
  '  this attribute specifies that the class is an extender
  '  (a single DLL can contain multiple extenders)
  '  the 1st argument is the the extender name, the 2nd argument is its
  '  description, the 3rd argument is the initial state (True=enabled)
  <VBMigrationExtender ("Property processor", _
  "Change the design-time  value of controls' properties", _
  True)> _
  Public Class PropertyProcessor
     ' all extender class must implement this interface
     Implements IVBMigrationExtender
     ' VB Migration Partner invokes this method  when the extender is loaded into memory
     Public Sub Connect(ByVal data As ExtenderData) Implements IVBMigrationExtender.Connect
        ' do nothing at connect time
     End Sub
     ' VB Migration Partner invokes this method many times during the migration process
     Public Sub Notify(ByVal data As  ExtenderData, ByVal info As OperationInfo) _
           Implements IVBMigrationExtender.Notify
        ' the Operation property specifies which  migration step we are in
        Select Case info.Operation
           Case OperationType.ProjectItemResolveSymbolTypesCompleted
            ' when this action is notified, all form controls have been parsed
            ' and their Properties collection  has been populated
              AssignProperties(info)
        End Select
     End Sub
     ' in this example we show how to change the Caption property of labels,
     ' buttons, checkboxes, optionbuttons to  uppercase
     ' and expand CommandButton controls by 1  pixel in all four directions
     Private Sub AssignProperties(ByVal info As OperationInfo)
        ' exit if this project item has no controls
        If Not info.ProjectItem.HasDesigner Then  Exit Sub
        ' iterate over all controls
        For Each comp As VBComponent In  info.ProjectItem.Component.Components
           ' convert Caption property to upper  case
           Dim type As String = comp.TypeName
           If type = "VB.Label" OrElse type = "VB.CommandButton" OrElse _
                 type = "VB.OptionButton" OrElse type = "VB.CheckBox" Then
              ' read the Caption property  (default is empty string)
              Dim caption As String = comp.GetPropertyValue("Caption", "")
              ' convert to uppercase and  re-assign to the component
              If caption <> ""  Then
                 comp.SetProperty("Caption", caption.ToUpper())
              End If
           End If
           ' enlarge CommandButton controls by 1 pixel in all four directions
           If type = "VB.CommandButton"  Then
              ' read Left, Top, Width, Height
              ' by default, command buttons are 1215x495 twips
              ' (for simplicity's sake we assume that form's ScaleMode is set to twips)
              Dim left As Single = comp.GetPropertyValue("Left", 0.0!)
              Dim top As Single = comp.GetPropertyValue("Top", 0.0!)
              Dim width As Single = comp.GetPropertyValue("Width", 1215.0!)
              Dim height As Single = comp.GetPropertyValue("Height", 495.0!)
              ' reassign back, but consider that 1 pixel = 15 twips
              comp.SetProperty("Left", left - 15)
              comp.SetProperty("Top", top - 15)
              comp.SetProperty("Width", width + 300)
              comp.SetProperty("Height", height + 300)
           End If
        Next
     End Sub
End Class

Notice that an extender DLL can contain one or more extender classes, each one devoted to a specific task. It is essential that the DLL contains only a single assembly-level VB6SupportLibrary attribute, as displayed in code above.

Individual extender classes can be enabled from the Extensions tab of the Tools-Options dialog box in VB Migration Partner. The first two arguments for the VBMigrationExtender attribute are the extender name and description and are displayed in the Extensions tab. If the third argument is true then you don’t have to manually enable this extender from inside the dialog.

You can now compile the DLL and deploy it in VB Migration Partner’s setup folder. Quit VB Migration Partner (if currently running) and launch it again. You can now migrate any VB6 project and see how controls are affected. You can also set breakpoints to debug your extender DLL, as you’d do with any .NET project.

 

Previous | Index | Next