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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I create a simple forms manager?

Forms & Screen

How can I create a simple forms manager?

by  ChrisRChamberlain  Posted    (Edited  )
If you give a form an object reference with a name clause, such as :-
[color blue]
DO FORM main NAME oMain
[/color]
there is a real risk that the object reference [color blue]oMain[/color] will go out of scope.


The code :-
[color blue]
PUBLIC oMain
DO FORM main NAME oMain
[/color]
ensures that the reference will be available anywhere in your application, but has the disadvantage that it uses a [color blue]PUBLIC[/color] variable.

It's also important to ensure that the object reference is immediately removed when the form no longer exists to avoid a 'dangling' object reference.


An alternative to using a [color blue]PUBLIC[/color] variable is to create a forms manager by using an object.

It's beyond the scope of this FAQ as to what other uses can be made of the object.

If you need more functionality than that as discussed, try using a collection instead of an empty or custom class. You will find further help on the use of the collection class through [link http://www.devx.com/codemag/Article/15698/1954]Doug Hennig's excellent article[/link].

In the root of your main.prg put :-
[color blue]
oForms = CreateObject([Empty]) [/color][color green]&& VFP 8.0/9.0[/color]
or[color blue]
oForms = CreateObject([Custom]) [/color][color green]&& < VFP 8.0
[/color]

When you launch a form, put :-
[color blue]
AddProperty(oForms,[oMain],.NULL.)
DO FORM main NAME oForms.oMain
[/color]
In the [color blue].UnLoad()[/color] or other suitable terminating event of the form main put :-
[color blue]
RemoveProperty(oForms,[oMain])
[/color]
to remove the object reference.


To determine if a form exists, put :-
[color blue]
IF VARTYPE(oForms.oMain) = [O]
[tab][/color][color green]* Code[/color][color blue]
ENDI
[/color]
All one needs to remember is to prepend the form name with the object name and all the normal rules of object referencing apply.

Another benefit is that although the primary reason for the creation of the object was forms management, you now have an object that can also be used for any other purpose anywhere else in your application.

Using the Search and Replace facility of VFP it is relatively painless to convert a project to this way of working.

Have fun. [smile]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top