Solved

Capture child window closed event in parent window

Posted on 2009-04-06
15
985 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared 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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

947 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

20 Experts available now in Live!

Get 1:1 Help Now