Re: How to get the parent form?

From: Stoitcho Goutsev \(100\) [C# MVP] (100_at_100.com)
Date: 07/15/04

  • Next message: DRaiko: "Re: How to get the parent form?"
    Date: Thu, 15 Jul 2004 11:02:09 -0400
    
    

    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.
    >
    >
    >
    >
    >
    

  • Next message: DRaiko: "Re: How to get the parent form?"

    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)