Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / July 2004

Tip: Looking for answers? Try searching our database.

How to get the parent form?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Deena - 15 Jul 2004 15:47 GMT
I want to get a reference to the parent form in the constructor of a child
form? I.e.  I want to do something like this :

FormX Child = new FormX();   //FormX inherits from the Form class

In Child's constructor I want find the parent form? I've tried .Parent and
.FindForm() to no avail! Reason for wanting to do this is so that a parent
form can keep a list of all it's child forms. So whenever a child form is
created it can notify it's parent that it exists.

public AbstractDialog()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

// If this is a child window then let it's parent know it was created

AbstractDialog parentForm = (AbstractDialog) this.Parent;    //
????????????????????????????????????????????????????????????????

if (parentForm != null)

{

parentForm.AddChildWindow = this;

}

}

where.....

// Adds child window to list

public System.Windows.Forms.Form AddChildWindow

{

// Add to list

set

{

formsList.Add(value);

}

}

Currently I am doing this in the parent form:

FormX Child = new FormX();   //FormX inherits from the Form class
Child.AddChildWindow = this;    //Look at above code

Which works but it would be cool if I could do it in the constructor.
Stoitcho Goutsev \(100\) [C# MVP] - 15 Jul 2004 16:02 GMT
Hi Deena,

By child form do you mean control? Anyways, in the constructor is too early.
Paren property is not set. It will be set as soon as the control is added to
the parent control collection.

So you can handle children ParentChanged event and look for the form there.
You can do that either by hooking the event or overriding OnParentChanged
method if you provide the class for the child control.

Signature

HTH
Stoitcho Goutsev (100) [C# MVP]

> I want to get a reference to the parent form in the constructor of a child
> form? I.e.  I want to do something like this :
[quoted text clipped - 59 lines]
>
> Which works but it would be cool if I could do it in the constructor.
DRaiko - 16 Jul 2004 09:09 GMT
Hi,

maybe i'm missing the point, but is it not possible to pass a parent
pointer into the ctor?

      ChildForm frm = new ChildForm( this);

and the ctor makes with it what it wants:
    public ChildForm( Form parentForm){
          if( parentForm != null){
               . . .
          }
    }

Well, the ctor is a not-default one. Sometimes this is not good.

HTH, Dima.

> Hi Deena,
>
[quoted text clipped - 69 lines]
> >
> > Which works but it would be cool if I could do it in the constructor.
Stoitcho Goutsev \(100\) [C# MVP] - 16 Jul 2004 19:33 GMT
Yes, it is possible, but the designer uses by default the constructor
without parameters. The question is whether you want ot write you own
designers for the components or want to go with the default one.

Signature

Stoitcho Goutsev (100) [C# MVP]

> Hi,
>
[quoted text clipped - 87 lines]
> > >
> > > Which works but it would be cool if I could do it in the constructor.
DRaiko - 21 Jul 2004 08:17 GMT
Hi Stoitcho,

if you look at the sample in the first mail after
"Currently I am doing this in the parent form ... " (at the end),
you will see, that it has nothing to do with the design mode.
So, your question "whether you want to write you own designers"
is already answered.

Regards,
Dima.

> Yes, it is possible, but the designer uses by default the constructor
> without parameters. The question is whether you want ot write you own
[quoted text clipped - 101 lines]
> > > >
> > > > Which works but it would be cool if I could do it in the constructor.
Stoitcho Goutsev \(100\) [C# MVP] - 21 Jul 2004 14:57 GMT
DRaiko,

Because you followed up my post where I suggested a control rather than a
form is more appropriate , because besides the MDI scenarios when one makes
a form child (TopLevel = false) actually the form becomes child window and
in this case control is more appropriate as lightweight control, your
suggestion will work only in a case when control is created at run time.

If one tries to drag and drop control wich has constructor without
parameters and with parameters designer will use the one without. If you
don't privide constructor without paramters VS will report an error when
trying to add the control.
The problem can be solved by creating a designer to that control. which will
initialize a property with the parent of the control. The same as MainMenu
designer does. However in a case of control (not component) where Parent
will be set as soon as the control is added to the control collection one
cas save some work processing ParentChanged event.

Yor suggestion work when
1. It is a form. Form's always are created at run time
2. It is a control and it is created at run time only. Design time for such
a control won't work.

Again, yes you sollution is ok, but you shouls've followed up the original
post. Form's doesn't have parents they have "owners"

Signature

B\rgds
Stoitcho Goutsev (100) [C# MVP]

> Hi Stoitcho,
>
[quoted text clipped - 112 lines]
> > > > >
> > > > > Which works but it would be cool if I could do it in the constructor.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.