Home > Archive > FoxPro Help and Support > January 2006 > reading events w/o using a form









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author reading events w/o using a form
cj

2006-01-27, 11:24 am

I've got a 3rd party ocx and of course it has events that it generates.
I don't need a user interface so I don't need a form for that. I can
generate the object for the ocx in a prg file but how would I write code
that reacts to the events it generates--I mean where would I put the
code if I don't have the form to access the methods from the properties
box to get a place to put the code.
Olaf Doschke

2006-01-27, 1:24 pm

Hi cj,

The simplest is to create a class based on the olecontrol class:

execute this line:
create class myOCXSubclass of "c:\temp\test.vcx" as olecontrol

Now you are promted to choose some OCX.

If you are talking about the socketwrench control, I think
it dislikes being instanciated without a form. I had some
error message during createobject() as I tried to simply
create it standalone.

Bye, Olaf.


cj

2006-01-27, 8:25 pm

Yes, I'm talking about SocketWrench but not the free version (don't know
what the difference is but my company bought me SocketTools Visual
Edition which includes this). I don't know what version your using.

I see the command you gave me creates what looks like a little form with
the control on it. So I assume I can then put code in the events of
this class I just created. It didn't seem that this class is included
in the project automatically. Do I then chose class libraries and add
the class I created to the project that way?

Then when I use createobject from the main prg. And do I still say read
events?



Olaf Doschke wrote:
> Hi cj,
>
> The simplest is to create a class based on the olecontrol class:
>
> execute this line:
> create class myOCXSubclass of "c:\temp\test.vcx" as olecontrol
>
> Now you are promted to choose some OCX.
>
> If you are talking about the socketwrench control, I think
> it dislikes being instanciated without a form. I had some
> error message during createobject() as I tried to simply
> create it standalone.
>
> Bye, Olaf.
>
>

Cindy Winegarden

2006-01-27, 8:25 pm

Hi CJ,

If you started with New on the class tab of a project your class library
will automatically be added to the project. Otherwise, browsing for it
always works. You can also drag/drop from an Explorer window, etc.

The "little" form is a design surface. You could, for example, but a group
of buttons (New, Edit, Save, Exit) on it and use that on lots of forms. Just
so you'll know, if you are working with a form you can put the class on a
form and it will show up visually.

You're right, you put code in the events/methods of your class. Did you know
you can create custom properties and methods? Lots of people have a
"DoStuff" type method on their forms and the button clicks just have one
line of code: This.DoStuff(). That way you can create a more generic form
class with your button group on it and each time you subclass it you can put
appropriate code in the DoStuff() method. The buttons in that instance
inherit the Click() code that calls DoStuff().

You can use CreateObject() in your Main program, or anywhere else you want
it for that matter. Use Set Classlib To ... to tell your program where to
find the class library if it's not already in the path (Set Path) of your
project. I've never worked with a non-UI event other than a timer. Maybe
somebody else has, or you can just plain experiment with Read Events.

--
Cindy Winegarden MCSD, Microsoft Visual FoxPro MVP
cindy_winegarden@msn
.com www.cindywinegarden.com


"cj" <cj@nospam.nospam> wrote in message
news:uHX43r3IGHA.1028@TK2MSFTNGP11.phx.gbl...
> I see the command you gave me creates what looks like a little form with
> the control on it. So I assume I can then put code in the events of this
> class I just created. It didn't seem that this class is included in the
> project automatically. Do I then chose class libraries and add the class
> I created to the project that way?
>
> Then when I use createobject from the main prg. And do I still say read
> events?



Josh Assing

2006-01-27, 8:25 pm

On Fri, 27 Jan 2006 14:50:45 -0500, cj <cj@nospam.nospam> wrote:

>Yes, I'm talking about SocketWrench but not the free version (don't know
>what the difference is but my company bought me SocketTools Visual
>Edition which includes this). I don't know what version your using.
>
>I see the command you gave me creates what looks like a little form with
>the control on it. So I assume I can then put code in the events of
>this class I just created. It didn't seem that this class is included
>in the project automatically. Do I then chose class libraries and add
>the class I created to the project that way?
>
>Then when I use createobject from the main prg. And do I still say read
>events?
>
>


Instead of createobject(" ... ") you'll use
newobj("MyNewClass","test.vcx")

that "form" is the vivsual class editor...

w/in the vivsual class you can edit methods/properties etc.


--- AntiSpam/harvest ---
Remove X's to send email to me.
Olaf Doschke

2006-01-28, 1:23 pm

Hi cj,

> Yes, I'm talking about SocketWrench but not the free version (don't know
> what the difference is but my company bought me SocketTools Visual Edition
> which includes this). I don't know what version your using.

I downloaded the free version, just so I could be of assistance.
Maybe it has this restriction and your class hasn't. We'll see. You
should test that early, before you add much code and then find out
you can't really do what you want. For me
o=NewObject("socketwrench"," e:\fox\9\winsock\soc
ket.vcx")
gives the error "object class is invalid for this container". But I
am able to create the class on a form with
thisform.newobject("o","socketwrench"," e:\fox\9\winsock\soc
ket.vcx")

> I see the command you gave me creates what looks like a little form with
> the control on it.

Indeed you have a container with the olecontrol on it, and can
put further controls on that if you like, but as you don't need
a GUI simply leave it as is.

> So I assume I can then put code in the events of this class I just
> created.

Yes.

> It didn't seem that this class is included in the project automatically.
> Do I then chose class libraries and add the class I created to the project
> that way?

The command I gave you created a classlibary "test"(.vcx) and a
class "myOCXSubclass" within c:\temp. I gave you the class creation
command, because it was easy to me and you to have the class and
classlibs created. That's the easiest way in a newsgroup post and you
can hardly go wrong. For showing you where to click and what to
enter into which dialogue, I'd need to do a video ;-).

Of course you should create the classlib test.vcx within your project
folder and not within c:\temp. And yes, the command does not add
the lib to the project, even if it would be in the project folder. As you
can have no or multiple projects open and the command has no para-
meter to tell in which project you want it or if you want the class in a
project it doesn' add it there. If the classlib you pass to the command
(eg " d:\myprojects\tcpips
erver\myclasses.vcx") already is part of the
project the class would of course be added to the project of course.
Sorry, I could have warned you. It's all to obvious to me, being used
to the vfp IDE.

The command has an advantage. Maybe you already noticed that the
command window has a history and stores commands you entered.
So you can scroll up and modify a command and repeat that. Also
notice, that if you open a project visually or create a class with the
"New" button of the project manager, the command window will
echo a command you also could have entered to do the same.

Of course with the New button it's clear to what project the class
should belong, and so a class created that way will be added to the
project at once.

> Then when I use createobject from the main prg. And do I still say
> read events?

Why shouldn't you not need this command? It's the event loop, re-
member? Only because you now run an activex control written in
some other language (C/C++/C#) that control doesn't take care of
events for foxpro. Otherwise it would be impossible to have two
activex controls running, as there only can be one event loop.

The concept of events is not limited to user interaction, it can also be
a connection request to a socket. Any event. So why should read
events only be needed with forms? Events is not a vfp concept, it's
a concept of windows (the OS).

Stop me, If I get too verbose ;-).

Bye, Olaf.


Olaf Doschke

2006-01-28, 1:23 pm

> For me
> o=NewObject("socketwrench"," e:\fox\9\winsock\soc
ket.vcx")
> gives the error "object class is invalid for this container". But I
> am able to create the class on a form with
> thisform.newobject("o","socketwrench"," e:\fox\9\winsock\soc
ket.vcx")

To be more specific: "socketwrench" is my subclass of the
socketwrench control. I created the class the same way I
recommended to you (based on "olecontrol") and I stored it in
e:\fox\9\winsock\soc
ket.vcx. So replace these parameters
accordingly and see if this works for you.

Bye, Olaf.


Olaf Doschke

2006-01-28, 8:24 pm

> I can generate the object for the ocx in a prg file but how would I write

> code that reacts to the events it generates


I wonder how you did this. There is an official way to define
an eventhandler class for a COM class. At the end of this posting
I give the needed instructions.

Important is, you end up with a non visual class in a prg and you
put your code in events/methods corresponding to COM class
events. When instanciating this alone you have no COM class,
you need to instanciate the native class (COM) and the eventhandler
class (VFP) and then bind the vfp class to the com class with
this:

oCOM = createobject("<<someCOM.Class>>")
oVFP = createobject("<<theVFPEventhandler>>")
EVENTHANDLER(oCOM,oV
FP)

To finally unbind the two objects you do:
EVENTHANDLER(oCOM,oV
FP,.T.)


Now here are the steps to create such a vfp eventhandler:

1. chosse VFP menu-> Tools -> Object Browser
2. click left uppermost toolbar button
"Open Type Library"
3. activate the tab COM Libraries
4.1 find your control type library
or
4.2 click Browse to find the corresponding
DLL or OCX (beware the open file dialogue
is set to dll files by default)
5. Click OK in the "Open Type Library" dialogue
6. Open a/the base node of the treeview with the
type library shown under "Classes & Members"
7. Open the node "Interfaces"
8. Create a new program (PRG) file
9. Drag an interface node to the empty editor
window and drop it there

You end up with some code like this:

x=NEWOBJECT("myclass")

DEFINE CLASS myclass AS session OLEPUBLIC

IMPLEMENTS ... IN "..."

PROCEDURE ....(...) AS ...
* add user code here
ENDPROC

...
ENDDEFINE

The x=Newobject() line is just an example, not needed.
You may also use another class than session, if you
don't want or need this as OLEPUBLIC class (vfp
com server).

Ok, especially for the Socketwrnch control I find two
Interfaces _DSocketWrench and _DSocketWrenchEvents
,
so you'd have two eventhandler classes. You should bind
these two eventhandler to the same socketwrench instance.

Now you can choose if you want to go the visual way by
subclassing based on olecontrol (as shown before) or by
the eventhandler. This way may be more complicated but
may work better/faster at runtime with fewer overhead.

Bye, Olaf.


Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com