Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello , I'm already working on an WPF application and I would like to know how can I check if the window is closed or not with ( X ) from another Window form.

For example , I have two form
the first form contains just a button so when I click on the button , it will show the second form and I should see in the output ( using `Console.writeline(form2.isclosed())` a message that show False, when I close the second form , the output should show a message True ( mean that the form2 was closed ) .

The two `Console.writeline()` should be in the first form not in the second ,
How can I do this?


C#
// Window form 1
           
            for (int k = 0; k <= listgrid.Count - 1; k++)
            {
                test  test = new test(listgrid[0]); // the new form ( Window form 2 )
                test.Show();
                if (test.IsClosed) // I need to check here if the window form 2 is closed or not
                {
                    Console.WriteLine("OKK Is closed after  " + test.IsClosed);
                    test.Show();
                }

            }



C#
// Second form
  public partial class test : Window
    {
        public bool test_close;

        public test(GridModel gridModel)
        {
            InitializeComponent();           
 
        }

        private void Window_Closed(object sender, EventArgs e)
        {
        //      base.Close();
            IsClosed = true;
            Console.WriteLine( " Ok " +IsClosed);
        }
        public bool IsClosed { get; private set; }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            IsClosed = true;
        }



What I have tried:

I have tried to just show the second form from a button in the first form.
Posted
Updated 3-Sep-20 4:53am

The simplest solution is to handle the second forms Closed event in the first: when you create the Form2 instance, you add your event handler beofe ryou show the the form, and your handler will be executed when Form2 closes.

It's pretty simple to do: Transferring information between two forms, Part 2: Child to Parent[^] shows the basics using a custom event - but the Closed event is part of a Form anyway, in both WPF and WinForms.
 
Share this answer
 
Comments
abdou_31 3-Sep-20 11:06am    
Thanks , but I can only pass parameters from form 1 to second form , not the contrariwise because I don't have ( `new form1().show()` in the second form ) I need just to get the result of event ( true or false ) from the second form and print it in the form 1 , I say `Console.writeline()` ( print ) just to get it for test, I need them later in other work.
OriginalGriff 3-Sep-20 11:59am    
You don't need to.
The event is handled by the non-closed form (Form1, which displays From2 to start with) automatically when Form2 closes. The Form2 instance that closed is passed to the event handler via the "sender" parameter, allowing your Form1 code to access it's properties at will.
1. Why are you using the Console.WriteLine in a GUI based app?
2. I think it would be better to use events in C# and delegate the notification to the parent Form instead of hardcoding/eyeballing the form to be closed before your proceed in C# code.

Handling and Raising Events | Microsoft Docs[^]\

This way you can trigger the event in the parent Form that the form is closing and the parent Form will "know" the form has closed, so you can proceed from there. Please check the documentation to learn more about that.

A little dirty approach will be to pass the parent Form instance in the test Form constructor and just before closing, call the method in parent Form to notify that the form has closed.
C#
WindowForm1 form1;
public test(GridModel gridModel, WindowForm1 parent)
{
    InitializeComponent();

    this.form1 = parent;
}

// later
private void Window_Closed(object sender, EventArgs e)
{
    // base.Close();
    IsClosed = true;
    Console.WriteLine( " Ok " +IsClosed);
    form1.FormClosed(this);
}
For this to work, accept the method in WindowForm1 to proceed;
C#
public void FormClosed(test form) {
   // your logic here
}

I repeat, this is a dirty approach; what's worse is that if you call the method this way, your secondary form has not yet closed (the method call has not returned) so you will be incorrect in your logic; I am sorry. :sigh:
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 3-Sep-20 11:01am    
Simple would be ugly; I am warning again, as that would contain a lot of bad practices, indeterministic branches, quantum fluctuations (okay, I lied with this one! ;-)...), but you get the point.

Learning the events in C# is the best bet for you here. But I have shared a sample code that would give an easier way to do this.

Note: From your code I am assuming that you want to open the test form after the current test form has closed. Which will be possible with the events more easily.
abdou_31 3-Sep-20 11:11am    
I would like to open the test form ( second form ) when I click on a button that exist in the first form.
After that I would like when I click on (X) of the second form ( test form ) , I would like to do something in the first form , I said as an example just print(Console.writeline(Window.isClosed)) should return true or false . That what I need.
Afzaal Ahmad Zeeshan 3-Sep-20 22:39pm    
And I have told you, it is easier to handle that logic with the events so that your Forms notify each other of the states and updates.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900