Solved

Capture child window closed event in parent window

Posted on 2009-04-06
15
1,025 Views
Last Modified: 2013-12-17
Hi

I am developing a WPF application. I wanted to know how can I capture child window closed event in parent window?
0
Comment
Question by:Prasannaganapayya
  • 8
  • 7
15 Comments
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24079142
Can a parent window know its child window closed?
http://www.codingforums.com/archive/index.php/t-36165.html
0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24079220
Or simple,
Declare a static public variable (integer) in the parent key (name it STATUS for example)
in the childreen windows, handler ClosedEvent to set STATUS variable to a particular value.
For example:
if form1 is closed it will set the STATUS to 1
if form2 is closed it will set the STATUS to 2
if form3 is closed it will set the STATUS to 3
.
.
then, monitor STATUS value by a timer or Background worker thread, when ever it set to a specific value do what you want to do.
0
 

Author Comment

by:Prasannaganapayya
ID: 24079274
I have wirtten a event in the child which gets called when the child window closes. I am subscribing for theat event in the parent window. Is this a correct approach? I am gettig object reference error
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:Prasannaganapayya
ID: 24079306
in the child window

 public event EventHandler SyncronizationClosedEvent;

        protected void SyncronizationClosed(object sender, EventArgs args)
        {
            if (args != null)
            {
                SyncronizationClosedEvent(sender, args);
            }
        }

 public event EventHandler SyncronizationClosedEvent;

        protected void SyncronizationClosed(object sender, EventArgs args)
        {
            if (args != null)
            {
                SyncronizationClosedEvent(sender, args);
            }
        }

And in the parent:

  syncronization.SyncronizationClosedEvent += new EventHandler(syncronization_SyncronizationClosedEvent);

Its throwing object reference error  in child window's SyncronizationClosed method
0
 

Author Comment

by:Prasannaganapayya
ID: 24079319
same methods are repeated above. On Child window closed event i m calling the event

 private void Window_Closed(object sender, EventArgs e)
        {
           
            SyncronizationClosed(this, e);
           
        }
0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24079367
I am subscribing for theat event in the parent window. Is this a correct approach?
Since you get the right result why not, do it your way...

I am gettig object reference error
you have called an object that is not referenced.
if you attach a code snippet containg where is the error rise i might help :)
0
 

Author Comment

by:Prasannaganapayya
ID: 24079441
Hi Saed,

Please find the code snippet attached. .Kindly have a look and suggest.
in the child window
 
 public event EventHandler SyncronizationClosedEvent;
 
        protected void SyncronizationClosed(object sender, EventArgs args)
        {
            if (args != null)
            {
                SyncronizationClosedEvent(sender, args);
            }
        }
 
private void Window_Closed(object sender, EventArgs e)
        {
           
            SyncronizationClosed(this, e);
            
        }
 
And in the parent:
 
  syncronization.SyncronizationClosedEvent += new EventHandler(syncronization_SyncronizationClosedEvent);

Open in new window

0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24079512

> private void Window_Closed(object sender, EventArgs e)
        {
           
            SyncronizationClosed(this, e);
           
        }


you passed 'this' to:
protected void SyncronizationClosed(object sender, EventArgs args)
        {
            if (args != null)
            {
                SyncronizationClosedEvent(sender, args);
            }
        }

But are you passing a nonreferenced (closed form) object ??
0
 

Author Comment

by:Prasannaganapayya
ID: 24079718
Its not closed yet right?
0
 

Author Comment

by:Prasannaganapayya
ID: 24079741
Or is there any alternate way to get the parent notified aboput child closed
0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24079743
This event occurs after the form has been closed by the user or by the Close method of the form. To prevent a form from closing, handle the Closing event and set the Cancel property of the CancelEventArgs passed to your event handler to true.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closed(VS.80).aspx
0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24079769
you handled the closed evet, but why not trying to handle the "Closing" instead ?
0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24079802
The Closing event occurs as the form is being closed. When a form is closed, all resources created within the object are released and the form is disposed. If you cancel this event, the form remains opened. To cancel the closure of a form, set the Cancel property of the CancelEventArgs passed to your event handler to true.

"Closed" differs from "Closing"
0
 

Author Comment

by:Prasannaganapayya
ID: 24083702
I tried using Closing event. It still gives me objecty reference error at

protected void SyncronizationClosed(object sender, EventArgs args)
        {
            if (args != null)
            {
                SyncronizationClosedEvent(sender, args);
            }
        }
0
 

Accepted Solution

by:
Prasannaganapayya earned 0 total points
ID: 24083801
I got it working..

ChildWIndow  = new Window.
ChildWindow.Closing = ClosingEventHandler
childWindow.Show()

private ClosingEventHandler()
{
     your refresh logic
}
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question