jtran007
asked on
C# Close MainWindow
Hi,
I have a MainWindow, after processing from this window, I create another Window2.
However I'd like to close the MainWindow bu tkeep Window1 open.
When I close Mainwindow, the Window2 is also closed. Please help.
Thanks,
JT
I have a MainWindow, after processing from this window, I create another Window2.
However I'd like to close the MainWindow bu tkeep Window1 open.
When I close Mainwindow, the Window2 is also closed. Please help.
Thanks,
JT
ASKER
Hi,
In WPF, I can do what I told you; but not in winform.Since I have limited memory, I
want to give back resources to window, after I use MainWindow. Your soluition still
keeps three window, eventhough one is hidden, but it still uses memory.
JT
In WPF, I can do what I told you; but not in winform.Since I have limited memory, I
want to give back resources to window, after I use MainWindow. Your soluition still
keeps three window, eventhough one is hidden, but it still uses memory.
JT
have an another window in the middle of the main window and window1. give it the main window functionalities and hide the main window.
now u can close the window like main window without closing the app.
now u can close the window like main window without closing the app.
ASKER
HI,
I should increase point value to 500 since it is not easy solution.
JT
I should increase point value to 500 since it is not easy solution.
JT
so what do u think about the given solutions.
i suggested one way of doing it.
Or u can implement the on exit event of main window
I can't remember the exact event name.
if u write that event u can stop closing the window
i suggested one way of doing it.
Or u can implement the on exit event of main window
I can't remember the exact event name.
if u write that event u can stop closing the window
In program.cs, pass your own implementation of ApplicationContext that only shuts down the app when all forms are closed:
Now you can Show() the second form BEFORE you Close() the main form and the app will stay open:
The app will close automatically when the LAST open form is closed...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyContext());
}
}
public class MyContext : ApplicationContext
{
public MyContext()
{
Application.Idle += new EventHandler(Application_Idle);
MainForm mf = new MainForm();
mf.Show();
}
void Application_Idle(object sender, EventArgs e)
{
if (Application.OpenForms.Count == 0)
Application.Exit();
}
}
}
Now you can Show() the second form BEFORE you Close() the main form and the app will stay open:
// in MainForm:
Form2 f2 = new Form2();
f2.Show();
this.Close();
The app will close automatically when the LAST open form is closed...
Use Form.Show() instead of Application.Run() to start your main window. Wait for your program to finish and call Application.DoEvents() to keep the messages pumping.
Make sure you don't have any lifetime issues with the second window getting destroyed when the first window closes. You can declare the Forms global somewhere as I have done in the main Program class.
You code to create the second window looks like this
using System;
using System.Windows.Forms;
namespace TwoWindows
{
static class Program
{
public static Form1 form1;
public static Form2 form2;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Program.form1 = new Form1();
Program.form1.Show();
while (Application.OpenForms.Count > 0)
Application.DoEvents();
}
}
}
Make sure you don't have any lifetime issues with the second window getting destroyed when the first window closes. You can declare the Forms global somewhere as I have done in the main Program class.
You code to create the second window looks like this
Program.form2 = new Form2();
Program.form2.Show();
@pchui....except that RAMPS CPU usage up because of your TIGHT loop. The ApplicationContext does the same thing without monopolizing the processor time...
@Idle_Mind... you are correct. My code needs some to break up the tight loop
Brings the CPU usage to 0% while idle on my system
while (Application.OpenForms.Count > 0)
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
Brings the CPU usage to 0% while idle on my system
I still wouldn't do it that way but technically it works...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Loops are bad !
I think the best solution is to create your own ApplicationContext, just as Idle_Mind said.
I think the best solution is to create your own ApplicationContext, just as Idle_Mind said.
Hi, you have to use this code
for(int k = 0;k < Application.OpenFormsk].Co unt;k++)
{
Application.OpenForms[k].C lose();
}
for(int k = 0;k < Application.OpenFormsk].Co
{
Application.OpenForms[k].C
}
hi, You have to write this code on MainForm_Closing() Event. ok
ASKER
Hi,
I have a scenario like this: I have 4 windows statically created durin the design time.
And they are dynamically shown as the process is going on.
First: win1 is shown to display instruction, then user click button 1 to go to win2 (win1 is closed now or later: my question )
Second: win2 display progress bar showing the initializastion, when done the program , win2 is closed,
and program display (show()) win3.
Third: win3 start collecting data, when done, win3 is close, and program display win 4
Fourth: win 4 display completion, and user click close, program exits.
From this scenario, which technique is the best one.
Thanks for your sharing knowlege with me.
JT
I have a scenario like this: I have 4 windows statically created durin the design time.
And they are dynamically shown as the process is going on.
First: win1 is shown to display instruction, then user click button 1 to go to win2 (win1 is closed now or later: my question )
Second: win2 display progress bar showing the initializastion, when done the program , win2 is closed,
and program display (show()) win3.
Third: win3 start collecting data, when done, win3 is close, and program display win 4
Fourth: win 4 display completion, and user click close, program exits.
From this scenario, which technique is the best one.
Thanks for your sharing knowlege with me.
JT
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks,
JT
JT
I hope this helps.