"Parent" or "Owning" Form of a "Sub-Form"

E

eBob.com

How does a "sub-form", i.e. one invoked by another form, determine anything
about the form which brought it into existence, i.e., I suppose,
instantiated it? I wanted to so something like this ...

MsgBox("called by " & Owner.Name)

.... but that throws a null reference exception. Parent.Name and
ParentForm.Name also throw null reference exceptions. Form1.Name works -
but that's not very flexible.

My MsgBox, above, is in the constructor of the "sub-form".

Thanks, Bob
 
R

rowe_newsgroups

How does a "sub-form", i.e. one invoked by another form, determine anything
about the form which brought it into existence, i.e., I suppose,
instantiated it?  I wanted to so something like this ...

MsgBox("called by " & Owner.Name)

... but that throws a null reference exception.  Parent.Name and
ParentForm.Name also throw null reference exceptions.  Form1.Name works-
but that's not very flexible.

My MsgBox, above, is in the constructor of the "sub-form".

Thanks,  Bob

If you want to show the message box from the constructor, you're most
likely going to need to pass in a reference to the creating from as a
parameter.

I wonder however if you mean you want to know what form showed the
"sub-form". In which case the message box should live in the "Show"
and "ShowDialog" method, but be warned it might only work if you use
the overloaded call that passes in the owning form.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
F

Family Tree Mike

If it is an MdiChild, the it can call MdiParent.Name. If as you suggest you
are just doing a ShowDialog type of call, then you could use a StackTrace
object.
 
E

eBob.com

If you want to show the message box from the constructor, you're most
likely going to need to pass in a reference to the creating from as a
parameter.

I wonder however if you mean you want to know what form showed the
"sub-form". In which case the message box should live in the "Show"
and "ShowDialog" method, but be warned it might only work if you use
the overloaded call that passes in the owning form.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/

Hi Seth,

What I am after is the name of the form which did the

Dim sform as new SomeForm

i.e. in SomeForm code I want to get the name of who New'ed me. Maybe the
same code will Show me maybe it won't. Maybe it already has maybe it never
will.

Although I ran into the problem in constructor code it seems to me that it
is a general problem. MsgBox has nothing to do with the problem, I was just
using it to experiment and to illustrate the problem. In general I would
say that it is a bad programming practice to try to get at any information
about your creator (unless it is explicitly passed). But I think that there
might be exceptions such as Location (which is what I was after).

What really makes me curious is what Parent, ParentForm, and Owner mean
since not a one of them means what I would have guessed.

Thanks, Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top