Subject Re: calling part of .wfm from another .prg
From Andy Taylor <andy.taylor.1959@outlook.com>
Date Wed, 29 Dec 2021 18:17:45 -0500
Newsgroups dbase.getting-started
Attachment(s) FormsUsingApp.prg

Milind,

Please study the normal method of opening a form. It uses a local variable called f.
Once the form wfm file has finished running then f is no longer in scope and is removed from memory automatically.

The form, while open, can see itself by using the form keyword but that has no meaning for any code that exists outside that form.wfm.
Imagine the chaos that would ensue if you had two forms open on screen and wanted to refer to the other form using form.

The solution to this (and, in general, for forms to be able to see and communicate with each other) is to refer to the form via an object that is always in scope... some people use _app, but I use a custom public variable app declared at the start of my applications.

So, lets assume that app already exists... Instead of opening a form by do myForm.wfm I might use:

app.Form1=new(MyForm)

From that point onward temp.prg can talk to the form via app.Form1

The very important point to note is that merely closing the form DOES NOT REMOVE IT FROM MEMORY because the reference from the app object still pointing to the form stops dBASE from releasing the memory assigned to the form.  In order to close the form properly you must null the form reference within the app object. Only then, with no external object references, will the form get released automatically by dBASE.

The prg code attached, FormsUsingApp.prg, should illustrate this technique.  Just read, run and watch.

Hope it helps,
Andy

> Scenerio:
> There are few .wfm statements in    function GRID1_onOpen()
> nCol = form.grid1.currentcolumn
> m_COL_name = \'COLUMN\'+ltrim(rtrim(str(1)))
>
> Want to call these 2 statements from external .prg say temp.prg
>
> Issue seen
> When temp.prg is called in  function GRID1_onOpen() get error message saying \'variable form not defined\'
>
> Any suggestions?
>
> Milind Nighojkar