473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Accesing parent form's sub

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

Nov 20 '05 #1
6 1087
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" <jo**@nospam.infovis.co.uk> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #2
* "John" <jo**@nospam.infovis.co.uk> scripsit:
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()
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
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 [MVP]" <RobTeixeira@@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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" <jo**@nospam.infovis.co.uk> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
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


Nov 20 '05 #4
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 [MVP]" <RobTeixeira@@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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" <jo**@nospam.infovis.co.uk> wrote in message
news:OT**************@TK2MSFTNGP11.phx.gbl...
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


Nov 20 '05 #5
You are correct.
Try my original code.

-Rob Teixeira [MVP]

"John" <jo**@nospam.infovis.co.uk> wrote in message
news:ur**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #6
It's a matter of timing. You create an instance of the new child form, and
its Load event occurs before you have a chance to set this property.
Shuffle things around a little, and it should work.

-Rob Teixeira [MVP]

"John" <jo**@nospam.infovis.co.uk> wrote in message
news:ei*************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Steve Jorgensen | last post by:
Yup, Steve's full of tips, but hey, it makes him feel important, right? Ok, here goes. I've been trying to improve encapsulation by putting code in the same object as the stuff it affects, so I...
3
by: Jeroen Ceuppens | last post by:
Hi, I have a problem accesing h, when i want to do h.Run() after the if else structure, i get the following error: (see below for code) C:\Documents and Settings\Eindwerk\Mijn...
5
by: user | last post by:
Hello I have Form1 : System.Windows.Forms.Form and some public controls in it: public System.Windows.Forms.ComboBox comboBox1; public System.Windows.Forms.ComboBox comboBox2; in the same...
12
by: Tom W | last post by:
What's the difference in these three?
3
by: julian_m | last post by:
A short piece of code which i'm having problems with: class ClsX{ var $F_name; var $F_array_of_obj= array(); function ClsX(&$obj, $name){ //Here I add an object reference to an array. Note...
4
by: Pk | last post by:
Hi, Im having a problem in forms. I have a mdi parent form named Main, and a child form named form1. >From Main form, i have a certain menu item that when clicked, it opens form1. But the...
13
by: Fernando Deutsch | last post by:
I have an html page with an iframe on it. Inside the iframe there is an img element. I am looking how to reference the iframe from the img element contained on it and cannot find a way to do it. I...
2
by: Daves | last post by:
code in a webcontrol's Page_Load event wants to grab value of a TextBox in the parent page; (TextBox) _textBox = (TextBox) this.Page.FindControl("TB_TextBox1"); string _value = _textBox.Text; ...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.