Tuesday, February 3, 2009

Using The Microsoft InteropForms Toolkit v2.0 To Display .NET Forms In A VB6 Application - Interop Forms Toolkit - VB 6.0

http://blogs.vbcity.com/shandy/articles/8912.aspx

The Microsoft InteropForms Toolkit v2.0 allows a VB v6.0 application to display .NET forms from within the VB6 application. This article is specifically for displaying forms and does not involve the use of .NET controls. I found information on how to do this on the internet a bit patchy, especially as I was converting existing forms rather than using the templates provided to create new .NET forms. All my experience is in using VB6 and VB.NET 2005.

  1. Download and install the Microsoft InteropForms Toolkit v2.0.
  2. Configure the .NET project you want to access from VB6. There are two basic methods you can employ to do this:
    • You can create a project from scratch using a ready made template installed by step 1. This template is located in the New Project dialog box under Visual Basic, Windows, My Templates and is called VB6 InteropForms Library.
    • You can convert an existing VB.NET project.
      • If the application type is not already a Class Library you will need to change it to a class library. You do this in the Project Properties, Application, Application type:
      • Ensure the assembly is COM Visible. You do this in Project Properties, Application, Assembly Information..., Make assembly COM-Visible.
      • Ensure that the application is registered for COM interop. You do this in the Project Properties, Compile, Register for COM interop.
      • Add a reference to the VB.NET project to the Microsoft.InteropFormTools.Dll library. By default this is located at c:\program files\Microsoft Visual Basic 2005 Power Packs\Interop Forms Toolkit 2.0\SDK\Libraries. You do this in Project Properties, References, Add...
  3. Configure the .NET forms you want to access from VB6.
    • Additional, new forms can be added using the Project, Add New Item, My Templates, VB6 InteropForm.
    • Existing forms can be modified.
      • Place the statement Imports Microsoft.InteropFormTools at the top of the form.
      • Place the statement _ on the line immediately before the form class definition.
      • The form class must have a constructor with no parameters.
  4. Within the .NET project create the InteropForm wrapper classes.
    • Use the Tools, Generate InteropForms Wrapper Classes option on the VS IDE menu bar.
    • This will create a folder in the Solution Explorer called Interop Wrapper Classes.
    • Each form class marked as an interop form will have a corresponding wrapper file created in the InteropForm Wrapper Classes folder. The corresponding wrapper file is named with the format .wrapper.vb. This file extends the original class.
    • If you change information such as the form class name you will need to re-create the InteropForm wrapper classes. I therefore advise re-creating the Interop wrapper classes before rebuilding the project.
    • Creating the InteropForm wrapper classes will mean you will lose any modifications you manually made to the InteropForm wrapper classes.
    • The re-creation of InteropForm wrapper classes will not remove any wrapper classes previously created for a form class that is no longer marked as an InteropForm. You must remove these manually.
  5. Build the .NET project.
  6. Configure the VB6 Project To Display The .NET Form.
    • Add a reference to the Microsoft Interop Form Toolkit Library.
    • Add a reference to the .NET project DLL.
    • Ensure that the .NET forms are properly cleaned up by the VB6 application.
      • Declare a global instance of the toolbox.
        Public g_InteropToolbox As InteropToolbox
      • Instantiate the toolbox, call Initialize on it for debugging support, and then signal the start of the application when the application starts (Sub Main or Main Form Load):
        Set g_InteropToolbox = New InteropToolbox
        ' Call Initialize method only when first creating the toolbox
        ' This aids in the debugging experience
        g_InteropToolbox.Initialize
        ' Signal Application Startup
        g_InteropToolbox.EventMessenger.RaiseApplicationStartedupEvent
      • Signal the shutdown of the application when the application ends (Sub Main or Main Form QueryUnload):
        g_InteropToolbox.EventMessenger.RaiseApplicationShutdownEvent
    • Add the code to call the .NET form.
      Dim oForm As ProjectName_FormClassName
      Set oForm = New ProjectName_FormClassName
      Call oForm.Show

No comments:

Mimmo97 Blog Archive