Solved

Capture child window closed event in parent window

Posted on 2009-04-06
15
976 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now