Getting reference to parent form

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I have posted this question several times but have received no reply. I have
a main form which contains a panel which in turn contains a child form, like
this; MainForm->MyPanel->ChildForm. The question is; how can I, from the
child form, get a reference to the main form? If I try to do dim frm as new
mainform then I get reference to a second instance of the mainform which is
not the same as the original instance of the mainform.

Isn't there a way to from child form do; childform.parent.parent or
something similar? Anyone has any ideas?

Thanks

Regards
 
* "John said:
I have posted this question several times but have received no reply. I have
a main form which contains a panel which in turn contains a child form, like
this; MainForm->MyPanel->ChildForm. The question is; how can I, from the
child form, get a reference to the main form? If I try to do dim frm as new
mainform then I get reference to a second instance of the mainform which is
not the same as the original instance of the mainform.

Have a look at the control's 'FindForm' method.
 
You may not be getting a reply becuase you are cross posting ...

James
 
Check the forms Parent property, or if its an MDI application, MdiParent.
They return the control thats containing this one.

/Dan
 
I have used this on my child form;

Dim myForm As Form = Me.FindForm()

Using watch I can see that Me is MyProject.ChildForm but the value of myForm
is also MyProject.ChildForm!! I would have thought that it would be
MyProject.MainForm.

Regards

From Watch Window;

Name, Value, Type
+ myForm, {MyProject.ChildForm}, System.Windows.Forms.Form
+ Me, {MyProject.ChildForm }, Contacts.frmClients
 
Oh, I'm sorry I missed that the panel parents a form.
Yes it this case FindForm won't work because it will return the form itself.

So, the form can be either top level and in this case it won't have Parent
(it might have Owner, though) or non top level in that case it should be
parented in a control or form. and the Parent property is set to reference
the parent control.

In your case you can do:

from inside the child form

this.Parent.FindForm(); and this will give you the MainForm.

Even if the parent of the form is a form it will work fine because FindForm
for forms returns the form itself.
You might want to check whether Parent is not null, though, in the cases
when the form is shown as a top level one



--
HTH
B\rgds
100
Stoitcho Goutsev (100) said:
Yes, it has and it works fine. What is the problem?

--
B\rgds
100

John said:
Has this been tested by anyone?

Regards
reply.
 
* "John said:
I have used this on my child form;

Dim myForm As Form = Me.FindForm()

Using watch I can see that Me is MyProject.ChildForm but the value of myForm
is also MyProject.ChildForm!! I would have thought that it would be
MyProject.MainForm.

'Me' is a reference to the current class instance. Calling 'FindForm'
for the /form/ will return the form, you are right.
 
I'm no expert, but the only way I've been able to do it is pass the parent
form as a parameter in the constructor of the child form.

Dale
 
ChildForms Parent property will return the MyPanel instance.
The Parent property of that instance will return the MainForm instance.

So to put it simply, use

Dim myMainForm as MainForm = ctype(Me.Parent.Parent,MainForm)

Hope this helps,
Marc
 
Back
Top