Accesing parent form's sub

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

John

Hi

I have a main form which has a panel which contains a child form using the
code;
mainform.myPanel.Controls.Add(childform)

So hierarchy is like this; MainForm->Panel->ChildForm. My question is how
can I access a sub in the main form, from the child form?

Thanks

Regards
 
You must make that sub friend or public (can't be private), and you must
pass a reference of the main form to the child form. The child form can
retain this reference (as a class field), and then use it to call the sub.
So, for example, if you create a myMainForm property on the child form, then
you can code:

mainform.myPanel.Controls.Add(childform)
childform.myMainForm = mainform

then, from inside childform you can do the following:

myMainForm.subName([paramater list goes here])

-Rob Teixeira [MVP]
 
* "John said:
I have a main form which has a panel which contains a child form using the
code;
mainform.myPanel.Controls.Add(childform)

So hierarchy is like this; MainForm->Panel->ChildForm. My question is how
can I access a sub in the main form, from the child form?

Untested:

\\\
DirectCast(ChildForm.Parent.FindForm(), MyForm).DoSomething()
///
 
Hi

Because I am already in mainform, I used childform.myMainForm = me. For some
reason this does not seem to work. The reference to myMainForm in childform
is nothing in the childform_load event.

Regards

Rob Teixeira said:
You must make that sub friend or public (can't be private), and you must
pass a reference of the main form to the child form. The child form can
retain this reference (as a class field), and then use it to call the sub.
So, for example, if you create a myMainForm property on the child form, then
you can code:

mainform.myPanel.Controls.Add(childform)
childform.myMainForm = mainform

then, from inside childform you can do the following:

myMainForm.subName([paramater list goes here])

-Rob Teixeira [MVP]

John said:
Hi

I have a main form which has a panel which contains a child form using the
code;
mainform.myPanel.Controls.Add(childform)

So hierarchy is like this; MainForm->Panel->ChildForm. My question is how
can I access a sub in the main form, from the child form?

Thanks

Regards
 
The only way I have been able to work this is so far is by doing;

frm as new mainform
frm.mysub()

in the child form. I suspect this creates a second instance of the main form
and not the one that the child form was originally opened in. Is this
correct? Is there a way to avoid creating a second instance of the main
form?

Thanks

Regards


Rob Teixeira said:
You must make that sub friend or public (can't be private), and you must
pass a reference of the main form to the child form. The child form can
retain this reference (as a class field), and then use it to call the sub.
So, for example, if you create a myMainForm property on the child form, then
you can code:

mainform.myPanel.Controls.Add(childform)
childform.myMainForm = mainform

then, from inside childform you can do the following:

myMainForm.subName([paramater list goes here])

-Rob Teixeira [MVP]

John said:
Hi

I have a main form which has a panel which contains a child form using the
code;
mainform.myPanel.Controls.Add(childform)

So hierarchy is like this; MainForm->Panel->ChildForm. My question is how
can I access a sub in the main form, from the child form?

Thanks

Regards
 
Back
Top