?
Solved

Capture child window closed event in parent window

Posted on 2009-04-06
15
Medium Priority
?
1,098 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

770 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