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
Set(ByVal value As String)
If m_custID <> value Then
m_custID = value
Private Sub LoadCustomer()
If Me.CustomerID <> “” Then
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.