Using the Interop Forms Toolkit in VFP9 – A Walkthrough
Part 1 - Installation, Control Creation & Events
Microsoft have recently released the Interop Forms Toolkit 2.0 at http://msdn2.microsoft.com/en-us/vbasic/aa701259.aspx . This toolkit allows developers to create Activex controls that can be hosted in VB as well as forms that can be called from VB. But this toolkit is not limited to VB alone. It can easily be used to create Forms and controls for use in VFP, as I will show.
First things first. Go and download the Installer from the page above. The only file you will need is InteropFormToolsInstaller.msi (5.2mb) and install it on your computer.
Now this toolkit is meant to be used with the full version of VS.NET as an ADDIN but that is something we really don’t need. The toolkit comes with some template files that will be used to do the heavy lifting in .Net. If you have the full version this will be installed as an Add-in. I use it in the free version of VBExpress.Net. which you can download from Microsoft.

Pic1
To enable the templates to appear in the Template selection in VB you will need to copy them to a particular location. So fire up explorer and navigate to the location as shown below. You need the ZIP files. VBExpress allows you to specify where it will look for your user templates.

Pic2
If you have the default directories, this will be
C:\Documents and Settings\bbout\My Documents\Visual Studio 2005\Templates\ProjectTemplates\Visual Basic\ for the templates which are :
VB6 InteropForm Library.zip
VB6 UserControl.zip
And the item templates go in
C:\Documents and Settings\bbout\My Documents\Visual Studio 2005\Templates\ItemTemplates\Visual Basic which are
VB6 InteropForm Library Info.zip
VB6 InteropForm.zip
VB6 UserControl Item.zip
Once you have done that the templates will appear in VBExpress whenever you opt for a new item.
Ok back in VBExpress select New and the New project dialog will appear. Select the VB6 Interop UserControl and rename your project as shown.

Pic3
VB Express will do its thing and create the files needed for your new user control. You now need to save the project to create the directories. So go ahead and save the project.

Pic4
One of the files created will be InteropUserControl.vb which we will rename to MyToolstrip.vb.
Pic5
Double click on it or Rt. Click and select “View Designer”. This will open the file ready for editing. From the toolbox on the left select the ToolStrip control and click on the pre-generated user control to add this control to the mix.

Pic6
The user control has no buttons so Rt. Click on it and select “Insert Standard Items”

Pic7
and standard buttons will be inserted for you..

Pic8
Now open the Code window (Rt click, “View Code”) and you will be presented with this area of code where you can add your own snippets. We need a way of exposing the events of the buttons so add the following bit of code which will create the events for each button.

Pic9
Next double click on each of the buttons in the Designer view to add an event handler as shown. Delegate the click to our newly created events. Intellisense will help.

Pic10

Pic11
That is all that is needed. You can run the control to see how it looks in VB Express. As a final step build the project and the template code will do all the work required to register the control.

Pic12
Move over to VFP. Create a new form and add an Olecontrol to it. In the dialog that will appear look for and select your control you just created and it will be added to your form.

Pic13

Pic14
Click the control and resize it to fit. Set its Anchor = 11
Now if you run the form you will see the control, but clicking any of the buttons does nothing. We need to wire in our code to handle the events we created earlier.
Double Click the control to open the VFP Code window and you will find the events we created showing up in the drop down list. Lets select the NewDocument Event first.
Pic15
and add code to handle the event when it occurs. For now just enter the code as shown.

Pic16
Do this for each of the events you created.

Pic17
Now run the form again and click the buttons. Your code will fire as shown. You have just succeeded in creating your first Interop User Control.

Pic18
Enjoy creating your .Net controls.
In my next post we will add in some other controls and expose properties. Till then have fun.