Reputation: 574
I have a pretty simple VS 2013 Windows Forms Application which is made up of 3 .vb pages.
Note - I 'inherited' this application and did not develop it myself.
On one page an Application Object is created.
Dim oVFP As Object
oVFP = CreateObject("VisualFoxPro.Application")
' Set up application object settings
oVFP.DoCmd("SET EXCLUSIVE OFF")
<... and so on ...?
On the User GUI Form I have an EXIT button whose Click method is on one of the other .VB pages.
This EXIT may be clicked at any time whether the oVFP object is 'working' or not.
Since the user might click on the Exit button while the oVFP is running, I'd like the btnExit Click method to check if oVFP exists and, if so, terminate it.
I tried:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
IF NOT oVFP is Nothing then
oVFP.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oVFP)
oVFP = Nothing
ENDIF
but since the oVFP is created on the other .vb page, it is not defined on the .vb code page with the btnExit method.
How can I, on the btnExit click method, test for the application object oVFP and, if it exists, terminate it.
Your assistance/advice would be gratefully appreciated.
Thanks
Upvotes: 0
Views: 3284
Reputation: 5737
In Windows Forms Apps you should not talk about pages, it's not a web app. So I assume you are talking about 3 form instances.
Well there are plenty of possibilities to check that. If your forms know each other, you can expose a property of one form whether the FoxPro object was loaded or not. Then, the other form can access that property and decide whether the app can be closed or not.
Alternatively you can create a static (shared in VB) member which can be accessed system wide (assuming you don't have a solution with projects NOT knowing each other).
Or, you can introduce a singleton which exposes that information. Comes quite close to the static thing.
Static members and singletons have architectual downsides but I don't think this is a problem for an app with 3 forms.
However, by using the right terms, you might google other approaches. Try it:
vb.net share information between forms
Upvotes: 2