Re: How to get the parent form?

From: DRaiko (draiko_at_rhenus.de)
Date: 07/16/04


Date: 16 Jul 2004 01:09:20 -0700

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.

"Stoitcho Goutsev \(100\) [C# MVP]" <100@100.com> wrote in message news:<#KtplxnaEHA.3664@TK2MSFTNGP12.phx.gbl>...
> 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.
>
> --
> HTH
> Stoitcho Goutsev (100) [C# MVP]
>
>
> "Deena" <dg@dg.com> wrote in message
> news:e48StqnaEHA.3480@TK2MSFTNGP11.phx.gbl...
> > 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.
> >
> >
> >
> >
> >



Relevant Pages

  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)