Interop Forms Toolkit - Your New Best Friend

Published 13 June 07 07:41 PM

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.

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Bernard Bout said on June 16, 2007 12:56 PM:

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.

# Beth Massi said on June 18, 2007 5:20 PM:

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

# VBTeam said on June 18, 2007 7:32 PM:

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

# yag: Community and Architecture said on June 20, 2007 8:46 AM:

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

# Beth Massi - Sharing the goodness that is VB said on June 28, 2007 5:48 PM:

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

# George Birbilis said on August 23, 2007 8:42 AM:

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)

# Elfmanne said on January 17, 2008 4:11 PM:

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??

# Nilesh said on January 18, 2008 6:07 AM:

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

# Toddap_MS said on January 22, 2008 5:06 PM:

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.

# Russell Sinclair said on February 6, 2008 4:48 PM:

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

# J2ThaD said on May 12, 2008 3:51 PM:

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

# Mahesh said on January 20, 2009 10:44 PM:

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?

# Beth Massi said on January 21, 2009 12:34 PM:

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

# Andy said on June 9, 2009 10:24 AM:

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

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

About Beth Massi

Beth is a Program Manager on the Visual Studio Community Team at Microsoft and is responsible for producing and managing content for business application developers, driving community features and team participation onto MSDN Developer Centers (http://msdn.com), and helping make Visual Studio one of the best developer tools in the world. She also produces regular content on her blog (http://blogs.msdn.com/bethmassi), Channel 9, and a variety of other developer sites and magazines. As a community champion and a long-time member of the Microsoft developer community she also helps with the San Francisco East Bay .NET user group and is a frequent speaker at various software development events. Before Microsoft, she was a Senior Architect at a health care software product company and a Microsoft Solutions Architect MVP. Over the last decade she has worked on distributed applications and frameworks, web and Windows-based applications using Microsoft development tools in a variety of businesses. She loves teaching, hiking, mountain biking, and driving really fast.

This Blog

Syndication

Page view tracker