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

Name of the form just opened 1

Status
Not open for further replies.

Toman

Technical User
Mar 30, 2004
190
0
0
CZ
Hi All,

I'm opening a form with command
Code:
 DO FORM SomeForm NAME oSomeName
In a given situation would be nice to read back the name (here "oSomeName") from the child form SomeForm. oSomeName.name works, but returns "SomeForm" of course. I'm looking for something which returns string "oSomeName"

The problem was discussed here formerly I suppose, but I can't find it. So I'm sorry for the question.

Toman
 
Toman,

In your example "oSomeName" is the string. You don't need to return it because the program (or whatever) in which you execute the line
Code:
DO FORM SomeForm NAME oSomeName
knows it already.

Hope that helps,

Stewart

Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Stewart,
Yes, the program with "DO FORM.." already knows the name, but the form SomeForm does not.
Form SomeForm knows or can easily find out its "native" name (thisform.name="SomeForm"), but not the name under which it was called. This is what I like to know.
Toman
 
Toman,

In a given situation would be nice to read back the name

What situation did you have in mind?

Do you need to know the object name from within the code of the form? In other words, you need to reference the object from one of the form's methods? If so, you can do that with THISFORM. You don't need to know the name of the object. In fact, it's better not to, as it makes the code more generic.

Or, do you need to know the object name from somewhere outside the form? In that case, you would have to store the object somewhere, such as in a public variable, or, better, in a property of some global object such as a form manager.

If this doesn't answer your question, perhaps you could explain the problem in more detail.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
I'd be interested to know why you want to do this...but how about calling it like this:
Code:
DO FORM SomeForm WITH "oSomeName" NAME oSomeName
- passing the oSomeName as a parameter to the Init event of the form?

Stewart

Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
The easiest way to do this is to pass the "parent" form to the child form.

Put a Parameter in the INIT() of the child form also create a property

lParameter pform
This.addproperty("parentform",pform)

When calling the child form from the parent, Do Form someform with This

Now from anywhere in the child form you can manipulate the parent forms methods, properties etc.

this.parentform.name &&&& name of the parent form

 
Mike and all others,
Mike said:
Do you need to know the object name from within the code of the form? In other words, you need to reference the object from one of the form's methods? If so, you can do that with THISFORM. You don't need to know the name of the object. In fact, it's better not to, as it makes the code more generic.
Yes this is my case – nearly. I don’t need to know the object name for executing code of that form, but for checking whether the name of this object for future referencing purposes is the same as expected.

Here is the whole story (short story at the end of my post).

There is a form in a project - let's say SomeForm, which is repeatedly called from many places in a project by means of command
Code:
 DO FORM SomeForm NAME oSomeName
I think it is preferable way according to this forum (just mentioning thread1252-827297).
Without that, one must make references to form name, which is not in fact name property of a form, but the name under which is form stored on disk – not very nice.

Form SomeForm also needs some child form (ChildForm) where are references to SomeForm (like oSomeName.text1. REFRESH())

After years I was adding some new functionality to this project and by mistake I have forgotten "NAME oSomeName" in DO FORM SomeForm.. command and ChildForm could not resolve the references. Here the question about real name of the form comes into existence.
In fact the biggest problem now is my English. It is behind my potential describe the situation more simply and more clearly.

Nevertheless I understand all your answers and recommendations and I thank you very much for them.
Toman

Modified form of my question:
For referencing purposes one needs the name of an object (here a form)

The name is:
[blue] SomeForm [/blue] if form is started with "DO FORM [blue] SomeForm [/blue]" or
[red] oSomeName [/red] if started with "DO FORM SomeForm NAME [red] oSomeName [/red]"

How to find the actual name (valid for referencing) from within a form SomeForm.
THISFORM.Name returns always "[blue] SomeForm[/blue] " and so is not a correct answer.
Please don't waste time with alternative solutions. There are many, I believe. For me is now only important if it is possible to get required object name or not.
 
For referencing purposes one needs the name of an object (here a form)"""

Oh! you want the name of the .scx (form)...

cFormname = SYS(1271,THISFORM)

 
for the Name Only with no extension or path

JUSTSTEM(SYS(1271,thisform))
 
For referencing purposes you need a reference of the form. Within the form that is easy, it's always THISFORM. From the viewpoint of code outside it's unknown, as long as the code didn't generate that form. Therefore the best solution is a form handler and a redesign of everything, if you don't have one and everything else is just seond best.

You have one thing you can check right before and after form generation: _SCREEN.Forms. I think the latest form will always be _screen.forms(1), this forms collection works this way, like a stack. So if you need a reference to the newly generated form, no matter if with DO FORM with or without the name clause, you can set formvar = _screen.forms(1) and you have a reference to that form. Of cause _screen.forms(1) will not always point to that form. And if a form does itself create a new form in it's init, _screen.forms(1) may not be the form you initally started, but the form started by that form.

So finally, the best thing you can do is use the NAME clause or have form classes, that you create by var = createobject("yourformclass"). Both ways you have references to the form.

Bye, Olaf.
 
two other points:

1. If you start form by DO FORM Someform NAME oSomeform it's the name of a VARIABLE holding a reference to the form. You can adress the form then via oSomeform from outside, as you can do from inside with THISFORM.

But it does not mean, that oSameform.Name is oSomeform, in fact oSomeform.Name is still Someform. You don't name the form itself, but a variable, holding the form reference.

2. If you start a form by DO FORM Someform without the NAME clause, it's name is the same as started with the NAME clause, you just don't have any variable holding a reference to that form. It's not the full truth, as experimenting with an scx shows me you still can adress such a form by it's name. But if you start a form twice without the name clause you are already in trouble.

Therefore you will be much better off, if you change your ways and don't handle your forms via their names, but by form references you store in variables or properties, eg a parent form could do this, starting a childform:


Code:
Local loChildform
do form childform NAME oChildform
This.Addproperty("oChildform",loChildform)

After this code has run, the variable loChildform is gone, as it's only a local var and releases after the method is through. But the form still has a reference to it's childform stored in it's property oChildform.

Bye, Olaf.
 
Gentlemen,

Thank you for interesting and comprehensive information and showing many points to think about. I am satisfied. No more problems. Today.
Tom

Olaf,

In existing project I can hardly think about changing a level of forms handling. But all your recommendations and tips are very valuable to me.
I also "worked" on a problem, but my conclusions slightly differ from yours at the point of naming forms as objects.

At the moment I should suggest (at least to myself) to use for referencing a form its "native" name (corresponding to the SCX file name and always obtainable with SYS(1271,thatForm) from anywhere.
In other words: now I should prefer to open forms with DO FORM without NAME clause, not seeing any advantage in using NAME clause because:
[ul]
[li]One can but also must reference form by variable used in NAME clause, reference by variable with the same name as the form is no more possible, [/li]
[li]as this thread implies, without special disposals (This.Addproperty("oChildform",loChildform) or so) there is no way to reconstruct the name under which was the form started and should be referenced in the future[/li]
[/ul]


Bye, T.
 
I can fully understand the problem it makes, to refactor an app to use a different approach of form adresing.

For a new app I'd rather recommend not adressing forms by their names, even if you plan on starting each form only once. You can't make general code adressing any form with that, if you adress forms by their names in code, you can only adress forms with that name, no others. Only references will give you the chance to adress forms more generic.

Bye, Olaf.
 
Let me reprhase my criticism. It's wrong you can't generically adress forms by their names. As Imaginecorp says you can use SYS(1271,thisform) or more general SYS(1271,formreference) to get the scx file name and then Jsutstem() on that. But you need a form reference for that, and within the form THISFORM is the most easy form referece you can have. This at least indicates already, that form references are the easier thing to use.

And if you start a form by DO FORM ... NAME xyz you can not only adress the form by xyz, you can pass xyz to a function and their adress the form by the parameter, you can still adress the form by THISFORM, you can set any other var to xyz and then have two form references. You can also determine a form reference by a name by searching the _Screen.forms() array until it returns a reference, for which oform.name is the name you expect. Or you put a reference to a collection with its name as the collections item key.

Bye, Olaf.

 
Olaf, we are near…

And if you start a form by DO FORM ... NAME xyz you can not only …
... also determine a form reference by a name by searching the _Screen.forms() array until it returns a reference, for which oform.name is the name you expect...
I am starting a form by DO FORM myForm NAME [red]xyz[/red].
Inside myForm there is a routine
Code:
 for i = 1 to _screen.formcount
	messagebox(_screen.forms(i).name)
endfor
What to change to get "[red]xyz[/red]" back. I can see myForm only
T.
 
You will also get the toolbar with the above code...

I somehow do not get what you are trying to do! why complicate the issue through all these, loops, _screen, Name etc etc,

when all you do is simply send the parent form to the child form, store the parent form object in a property of the child form. Now Any and All information of the parent form can be accessable to you...
 
You still didn't get the point, that the NAME clause does not set the name of the form, but specifies the Name of a variable, that will have a reference to that form. You don't get "xyz" by your loop, because that is not the name of the form, but of a variable having a form reference to your form. What really matters is neither the form name, nor that variable name, it's the reference to the form, that is the value of that variable, and that can be stored somewhere to get it back with any other variable name you have in any code that needs a reference.


So wherever you want to adress this form you can do that by using that value of xyz. So in the first place you better store that value somewhere you can get it back from everywhere you need it, an collection would be nice for this.

Code:
_screen.addobject("oForms","Collection")

Now whenever you create a form you simply do
Code:
DO FORM yourform NAME variablename
_screen.oForms.additem(variablename,"yourform")

Now you can release tht variable, it's not the form. And you can address that form by getting the reference from that collection back by it's key name "yourform".

Code:
Local loYourForm
Try
loYourForm = _screen.oForms.item("yourform")
Catch
* if the form was already closed, create a new one:
Do Form yourform NAME loYourForm
_screen.oForms.additem(loYourForm,"yourform")
Endtry

loYourform.Caption = "This is a new Caption"

In this sample code, the value is retrieved back from the oForms collection, but even better: if the oForms collection does not have that form anymore, it's started again and it's reference is stored in the collection.

As you search some code that you can put after each DO FORM, that is a solution that should work for you. You can choose the name for which you'd like your form to be found in the collection, if some code wants to adress that form.

Bye, Olaf.
 
Olaf,
Thank you for your detailed explanation and your patient too. Star for you.
Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top