Write Fox code in Visual Studio that interacts with your VB.Net code

Run the Fox code in Allowing Optional parameters in your COM objects (or the one in Blogs get 300 hits per hour: Visual FoxPro can count.) to create a versatile Fox server called T1.DLL. You can use that server in VS to execute Fox code.

The VB.Net sample form below calls the Fox server, executes some code which calls back into the VB.Net code.

Start VS, choose File->New->Project->VB->Windows Form Application. Add a reference to (Project->Add Reference->Browse and navigate) to the VFP server T1.DLL that you built.

Choose File->New->File->General->Text File. Call it Test.Prg. Paste this code:

PROCEDURE test(p1, p2,p3)

      RETURN PROGRAM()+ ;

      " p1="+p1+ ;

      " p2="+p2.VBProperty+;

      " p3="+p2.VBFunc(p3)

Save the Text file in the <ProjectLocation>Bin\Debug directory so it can be found in the same dir as your app.

Dbl click the form and paste the code below. Hit F5 to run the code: Click the button. 2 MessageBoxes appear:

“here I am in VBFunc 3rdParm”

“TEST p1=first parm p2=VB Property p3=VBFuncRetval”

Notice that this sample compiles a Fox program and calls it, passing in the VB Form itself as a parameter. Fox then calls back into VB to evaluate a property and invoke a method.

Without rebuilding anything, change the Test.PRG code, then click the button again. You’ve just changed the behavior of the application without shutting it down! Very useful, especially in web server scenarios.

You can do similar things in

  • C# (pass in “this”)
  • Outlook, PowerPoint, Excel, Word … (pass in “Application”)

Public Class Form1

    Dim WithEvents oBtn As New Button

    Public VBProperty = "VB Property"

    Public Function VBFunc(ByVal VBFuncParam As String)

        MsgBox("here I am in VBFunc " + VBFuncParam)

        Return "VBFuncRetval"

    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.oBtn.Text = "Click me"

        Me.Controls.Add(oBtn)

    End Sub

    Sub btn_click() Handles oBtn.Click

        Dim ot1 As New t1.c1

        Dim cPath As String = ot1.MyEval("curdir()") ' current dir (same drive)

        ot1.MyDoCmd("set path to " + "'" + cPath + "'") ' set fox path to find the prg

        ot1.MyDoCmd("compile 'test.prg'")

        ' we're passing the form to Fox which will call back to the form

        MsgBox(ot1.MyEval("test(p2,p3,p4)", _

                          "first parm", Me, "3rdParm"))

    End Sub

End Class