Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Capture child window closed event in parent window

Posted on 2009-04-06
15
Medium Priority
?
1,123 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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)…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 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