Re: How to get the parent form?
From: Stoitcho Goutsev \(100\) [C# MVP] (100_at_100.com)
Date: 07/15/04
- Previous message: Deena: "How to get the parent form?"
- In reply to: Deena: "How to get the parent form?"
- Next in thread: DRaiko: "Re: How to get the parent form?"
- Reply: DRaiko: "Re: How to get the parent form?"
- Messages sorted by: [ date ] [ thread ]
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.
>
>
>
>
>
- Previous message: Deena: "How to get the parent form?"
- In reply to: Deena: "How to get the parent form?"
- Next in thread: DRaiko: "Re: How to get the parent form?"
- Reply: DRaiko: "Re: How to get the parent form?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|