Interop Forms Toolkit - Your New Best Friend

Interop Forms Toolkit - Your New Best Friend

Rate This
  • Comments 21

I've been heads down this week (re)learning an older technology -- Visual Basic 6! With all the things to focus on like this, this and this you're probably asking "Um, Beth, WHY!?". It's because I've really been learning the Interop Forms Toolkit 2.0 in order to bring you some killer videos on the subject. The Interop Forms Toolkit makes interoperating with .NET forms and user controls from VB6 (and Fox) apps a total snap! I'm really impressed with how easy it is. The toolkit provides a library and code templates that does all the heavy lifting for you so you can concentrate purely on the task at hand. (NOTE: The Interop Forms Toolkit 2.0 is a Visual Studio Add-In and does not run under the Express editions of Visual Studio.)

Why would you want to use it? Well lets say you have a 10 year old Visual Basic (or Visual FoxPro) line-of-business application that needs new features that only .NET can provide, or it's just brain-dead simple in .NET and you're crying because of the effort involved with VB6. Well that's where the toolkit comes in. Instead of converting your entire codebase over to .NET (yea, that'll happen), you can take a phased migration strategy to your application -- even if the application is a monolithic one. This is because the toolkit allows you to seamlessly interoperate between VB6 (COM) and .NET code. .NET controls look like ActiveX controls to VB6.

Let's look at an example. Once you install the toolkit you're ready to start creating .NET control assemblies that can interoperate with VB6. (You can also create complete .NET forms, but the advantage of creating user controls (new to version 2.0 of the toolkit) instead is that they can use RegFree-COM which allows you to deploy your assemblies along with your VB6 exe's via ClickOnce Technology. ) When you create a new project, select the template located under "My Templates" called VB6 Interop User Control which will create a control library that you can interoperate with VB6 applications. This template sets up the proper interop code you need as well as the manifests that enable RegFree-COM.

If we double-click on the InteropUserControl you'll notice some ComClass attributes as well as a region called "VB6 Interop Code".

 

So I have a VB6 application that we want to extend. For this example it's just a simple form with a grid that pulls up customers from an Access database. A new requirement is to be able to edit the customer details below this grid so we'll extend the VB6 application with a .NET user control. We can easily create this user control in .NET and connect to the same Access database using the drag-and-drop databinding of Visual Studio 2005 as shown in my videos.

Of course this is a very simple example. You are not limited to data-access controls. You have the entire .NET Framework here at your disposal including the BackgroundWorker for multi-threading and .NET Web Services -- things that in VB6 are difficult or impossible to do. 

When we design our .NET user control, any public property or method we create on the control will be exposed to VB6. I'll add a public property called CustomerID and in the setter I'll load the customer record from our database. I just create a simple parameterized query (like I did here) on the CustomerTableAdapter that passes the CustomerID.

'Please enter any new code here, below the Interop code

 

Private m_custID As String

Public Property CustomerID() As String

    Get

        Return m_custID

    End Get

    Set(ByVal value As String)

        If m_custID <> value Then

            m_custID = value

            LoadCustomer()

        End If

    End Set

End Property

 

Private Sub LoadCustomer()

    If Me.CustomerID <> "" Then

        Me.CustomersTableAdapter.FillByCustomerID( _

                Me.CustomerDataSet.Customers, Me.CustomerID)

    End If

End Sub

Now build the solution. In development, this will register your interop user controls into the registry so that you can use them as ActiveX controls in Visual Basic 6. So back in the VB6 IDE, just add a reference to your new components by selecting "Project -- Components" (or Ctrl +T) and selecting your library. This will add the components to the VB6 toolbox.

Then you can drag-and-drop the user control from the toolbox onto the VB6 form. Jump to the code-behind in VB6 and add the code to set the CustomerID on the user control. You get intellisense on the public properties and methods.

Running the VB6 app we can now access our .NET control just like any other control on the form. The interop forms toolkit helper methods even take care of managing tabbing and focusing of the controls for you.

 

Consider performing a phased migration to your current LOB applications instead of a costly rewrite using the Interop Forms Toolkit 2.0. As you can see it allows you to quickly get .NET functionality into your current applications. Your users will need the .NET framework installed on their machines and the .NET user control assemblies registered, however with RegFree-COM and ClickOnce Deployment, these issues can be easily solved.

Leave a Comment
  • Please add 2 and 2 and type the answer here:
  • Post
  • Hi Beth

    I also read Craig Boyd's article about this toolkit. You are wrong when you say it does not work with the VS Express Editions. It can and does.

    Yes you cannot use it as an add-in in the Express editions but you can use the templates provided.

    All that is needed is to copy the templates to the correct Template directory and they will appear in the Express editions.

    This is how I initially tested the toolkit by downloading it from the original site here:

    http://blogs.msdn.com/vbteam/archive/2006/11/02/interop-roadmap-usercontrols-mdi-and-data.aspx

    This above file is just a template with no add-in.

    I am yet to figure out how to handle events but that is another story.

    Cheers.

  • Hi Benard,

    "Officially" add-ins are not supported in the Express Editions. I'm glad you were able to get it to work though <wink> <wink>.

    Cheers ;-)

    -B

  • Yes, officially the Toolkit is not supported on Express since InteropForms require an Addin - but you're correct that Interop UserControls will work without issue on Express (since Express includes the full compiler).

    Check out the top part of this thread for a walkthrough on events: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1102737&SiteID=1&mode=1

    Jonathan Aneja

    Program Manager, VB Team

  • I'm here in Prague , have to head back to the conference for the Q&amp;A session, but spent some time

  • One of the features of the latest Interop Forms Toolkit is the ability to develop UserControls in addition

  • Note that the 2.0a forms tookit installer (not the redistributable package), doesn't install on Vista, unless you run it from a command-prompt running from an administrator account (right click command prompt icon at strart menu and select run-as administrator that is)

  • Is it possible to have a .net Form be the parent form in a VB 6 application?

    If so, is is possible then to host both VB 6 forms and .Net forms as a mdi child to the .Net parent form??

  • Hi

    is it possible to load interop controls in VB6 dynamically at the run time, without first adding them to a form at design time?

    Thanks

  • On comment from Elfmanne -> The answer is no, although what you can do with the 2.0 toolkit is create interop usercontrols which you can host in a VB6 mdi child form which should work for you.

    On comment from Nilesh -> The VB6 support for this was using controls.add -> This works in the same with with activex controls, so I think the answer is yes, although it really depends on what you are trying to do.

  • So do you need to install the redist MSI on computers when you install your applications?

  • Beth (or anybody else),

    I was able to get a VB2008 User Control to work on a VB6 form (thanks for the videos), however, I cannot get the User Control registered properly on another machine that doesn't have VB2008 installed.  So my question is, how do you get a VB2008 User Control to work on PC that only has VB6 installed?

    Thanks,

    J2ThaD

  • Hi All - I have an access form (with SQL database). For future developments I would like to create new forms in .NET, but not found any good way to integrate access form and .NET form.

    Does this toolkit help me with this or is it just for VB6 applications?

  • Hi Mahesh,

    I'm not familiar with the architecture of Access forms. But if you can add ActiveX controls to your Access forms then it will probably work. The toolkit just makes it easy to expose .NET user controls to COM. I know that it does work with Visual FoxPro clients.

    HTH,

    -B

  • When I try and reference the InteropForm in my VB6 app I get an automation error :- Invalid pointer??

  • I've developed a user control via the interop forms toolkit and added a single string property to the class. My target client app is using MS Access. In designer mode, I can set the property, however in code tied to the form I cannot access the property. If I add a reference to the Access project, I can instantiate an instance of the control in code and see the custom property. What do I need to do to have the property on an instance of the control that's beeen created by the design environment?

Page 1 of 2 (21 items) 12