Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with instance Urgent Please 1

Status
Not open for further replies.

koukouroukou

Programmer
Feb 6, 2002
38
GR
Lets say tha my Form1 calls :
dim withevents cl1 as cl
set cl1 = new cl

and do all the stuff that i want .

What if i want to call from another application(or Dll) the cl and raise events; ( i want my form1 to react on the raise event of the second instance of the class)
In other words is there any way to use the same class for 2 different applications ,keep the stored values and also both applications react whith a raise event;
 
You can't do exactly what you are asking for however there are alternatives. Your basic problem is you need one application to somehow send a "message" to another application that "Something happened on my side and I gotta let you know".

One alternative is to use the WinSock control and pass messages to and from each of the applications via windows sockets.

Another alternative is to use sub classing and post messages to each other's window handle using the API.

Unfortunately neither one of these options are for the beginner and require too much explanation to post here. Do some searching on the internet for VB sites. You'll find lots of examples on how to use both.

--Adam
 
Yes, you can do exactly as you describe. I posted some pretty straightforward code to demonstrate this in this forum in the last few months, which happened to use a factory class to achieve this. Have you tried doing a keyword search?

If you can't find what your looking for, just give a shout here and I'll try and dig out the relevant example.
 
In fact, here's an example.

Here are the steps:

Create a new ActiveX EXE project
Add a module, and two classes (in my example called clsSystemGlobal and GlobalMaker)

clsSystemGlobal's instancing property should be set to PublicNotCreatable, and GlobalMaker's should be set to MultiUse.

Now drop in the following code into the module:
[tt]
Option Explicit

Public gSystemVariable As clsSystemGlobal
Public gObjectCount As Long
[/tt]
Into clsSystemGlobal place:
[tt]
Option Explicit

Public Event GlobalEvent()

Public Sub RaiseGlobalEvent()
RaiseEvent GlobalEvent
End Sub
[/tt]
Into GlobalMaker place:
[tt]
Option Explicit

Public Property Get clsSystemGlobal() As clsSystemGlobal
Set clsSystemGlobal = gSystemVariable
End Property

Private Sub Class_Initialize()
If gSystemVariable Is Nothing Then
Set gSystemVariable = New clsSystemGlobal
End If
gObjectCount = gObjectCount + 1
End Sub

Private Sub Class_Terminate()
gObjectCount = gObjectCount - 1
If gObjectCount = 0 Then
Set gSystemVariable = Nothing
End If
End Sub
[/tt]
And finally an example project (you'll need a form with a command button):
[tt]
Option Explicit
Private WithEvents Test1 As clsSystemGlobal
Private myFactory As New GlobalMaker 'Keeps object in scope

Private Sub Command1_Click()
Test1.RaiseGlobalEvent
End Sub

Private Sub Form_Load()
Set Test1 = myFactory.clsSystemGlobal ' use factory to create object
End Sub

Private Sub Test1_GlobalEvent()
MsgBox "This is program 1"
End Sub
[/tt]
You'll want to create more than one copy of this test program, and you should choose a different message for the messagebox to output.
 
Hi strongm ,,,

I tryed your example but it does not work.

Do i have to do another porject or what ?
thanks
 
As I said just before the final block of code:

>And finally an example project (you'll need a form with a command button)

In other words, yes. I guess I missed off the fact that you'll need to add a reference, in this new project, to the ActiveX exe you created in the first part.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top