C# application exits when I dispose - c#

Basically I have a Login window which should close once the user logs in and show another window, for now I've just hid it(Form.Hide()) however I do not wish to take unnescesary system resources and I don't need the login window after I already logged in.
this is the code snippet where I perform the operation:
MainWindow w = new MainWindow();
TimeRegisterApI.Instance.Windows.Add(w.Text,w);
TimeRegisterApI.Instance.Windows[w.Text].Show();
this.Dispose();
Windows is a dictionary that stores references of forms with their title as the key.
TimeRegisterApi is a singleton.
Basically what happens is that my application exits after I login instead of just disposing the login window, when I want it to dispose(close and go to the garbage collector.)
I know that having the title as key might cause duplicate key entries but in my current design it's no problem.

You have to make your window form login as a modal form which parent is your mainform
it will allow you to wait an answer (like a savedialog) and return to your main window form)
this is your main form so you exist

ok I fixed the problem, basically what happens is that when the window that is run from program.cs at application.run(window) gets disposed it closes the program because it's the main window, I think it would be possible to do an e.cancel on the dispose event of this window, however I solved the problem by running the window in a new application.run like this:
from program.cs, relevant lines
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LoginWindow());
start();
}
public static void start()
{
if (TimeRegisterApI.isLoggedIn())
{
MainWindow w = new MainWindow();
Application.Run(w);
}
}

Related

Opening another form in an MDI container

Please refer to the diagram below:
I'm looking for the best way from one form to open another form. Once the menu form is open, I want to close the login form. I have tried to do this, but I got lost when I try to call it as a child of the MDI container.
The reason your main form isn't showing is because once you close the login form, your application's message pump is shut down, which causes the entire application to exit. The Windows message loop is tied to the login form because that's the one you have set as the startup form in your project properties. Look in your "Program.cs" file, and you'll see the responsible bit of code: Application.Run(new LoginForm()). Check out the documentation for that method here on MSDN, which explains this in greater detail.
The best solution is to move the code out of your login form into the "Program.cs" file. When your program first starts, you'll create and show the login form as a modal dialog (which runs on a separate message loop and blocks execution of the rest of your code until it closes). When the login dialog closes, you'll check its DialogResult property to see if the login was successful. If it was, you can start the main form using Application.Run (thus creating the main message loop); otherwise, you can exit the application without showing any form at all. Something like this:
static void Main()
{
LoginForm fLogin = new LoginForm();
if (fLogin.ShowDialog() == DialogResult.OK)
{
Application.Run(new MainForm());
}
else
{
Application.Exit();
}
}
if you develop WPF form, you can use 'http://wpfmdi.codeplex.com/'. or in Win Form you could play with form such that you want
try this
//global form
LoginForm login = new LoginForm();
public void menuOpen()
{
if(login.Visible)
login.Close();
}

C# unload and load form / hide and close

I am coding a simple sidescroller in C# using Windows Form Applications. I want it so when the player touches an exit point, this immediately loads a new level.
To implement this, I use the following code:
if (player.Bounds.IntersectsWith(exit.Bounds))
{
Form2 myNewForm = new Form2();
myNewForm.Visible = true;
this.Hide();
}
This works. However, it loads numerous instances of form2 - I only want it to load once. I don't know how to write this (sorry, I'm a newbie - it took me a while just to write this code!).
Also, loading a level via a new form is inefficient. Is there a way to unload the open form to load the next one in the same window/instance, rather than creating another separate window?
Sorry if this is unclear. I've done my best research + I'm new. Please don't mention XNA! Thanks.
You need a small modification to your project's Program.cs file to change the way your app decides to terminate. You simple exit when there no more windows left. Make it look like this:
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var start = new Form1();
start.FormClosed += WindowClosed;
start.Show();
Application.Run();
}
static void WindowClosed(object sender, FormClosedEventArgs e) {
if (Application.OpenForms.Count == 0) Application.Exit();
else Application.OpenForms[0].FormClosed += WindowClosed;
}
}
Now it is simple:
if (player.Bounds.IntersectsWith(exit.Bounds))
{
new Form2().Show();
this.Close();
}
You can use Application.OpenForms[] collection to retrieve the Opened form instance and then Show it.
Try This:
Form2 frmMyForm = (Form2)Application.OpenForms["formName"];
frmMyForm.Show();
The actual problem is not in your form being loaded multiple times, but in your game logic not being suspended when the end of the level is reached. It means that your game keeps playing an old level when a new level is already loaded.
If Form1 is the main form then your whole application will shutdown. You need bootstraper which will be the entry point of your application, not the Form1. If you do that your Form1 will be a child in the same sense as will be Form2. You can open and close them without shutting down the application. If you don't know how to do that, just create another empty form, let's call it Main and make it the starting form of your application. Then hide it and open Form1 from Main form as Modal. Then when you complete level in Form1, close Form1, the code flow will return to Main form and you'll spawn Form2 from Main form as Modal. You'll have fully predictable logic, where all forms are opened from a single controlled place.

Closing Form In C# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# Working with mutliple Forms
I have two forms in C#. I want to close one form and show other. The code is as follows:
AMBR A = new AMBR();
this.Close();
A.Show();
The current form is my main form. It shows my second form, but than closes both and my program stops. I know another standard approach is to hide the form, but my main form only has a logo and loading bar. It don't need any interaction with the user. When I hide it, after the second form is closed the program remain open (as seen in the task manager) and continues to occupy resources. I want the main form to close and second form to remain open.
Open your "Program.cs". Modify the code to be as follows, where SplashFrm is the current Form that is being created in your Application.Run call
static class Program
{
private static EventHandler idleTemp;
private static SplashFrm splash;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
splash = new SplashFrm();
splash.Show();
idleTemp = new EventHandler(Application_Idle);
Application.Idle += idleTemp;
Application.Run(new AMBR());
}
static void Application_Idle(object sender, EventArgs e)
{
splash.Close();
Application.Idle -= idleTemp;
idleTemp = null;
splash = null;
}
}
Then, after AMBR is successfully loaded, call Application.RaiseIdle(null); and your splash will be closed and cleaned up.
No you cannot close the main form and maintain the second form opened. But you can hide it.
this.Hide();
EDIT:
Another solution could be use the second form as the main form and make it invisible while the second form (splash form) is opened.
So:
this.Visible = false;
//Show the second form
There is an example on the Application.Run MSDN page that explains how you can inherit from ApplicationContext to make your program end only when the last form has been closed, and not just once the main form is closed.
Have you tried this.Hide(); instead of this.Close();?
The problem probably happens because your main method is creating the first form and waits for it to close. when it closes the main proceeds and reaches the end which result in your application shutdown.
either make the main method create the second form or dont close the first one and just hide it + hide the taskbar icon.
It sounds like you have the wrong main form. You should change the other form that you are opening to be your main form, and the form that is currently your main form should be opened by the other form. When your current main form is closed, have it Show your new main form. Have your new main form hidden when the program starts.

Threads are still running in C#

I have 2 forms: signin and control_panel. After signin done I'm hiding this form by this.Hide() function and same time I am making new object of control_panel form and showing it by newobj.Show();. But when I am closing directly control_panel form, I am seeing first form thread are still running. I am closing it by stop_debugging button. How will I close every threads or whole program exit simultaneously.
The thread for your first form is still running because you're only calling this.Hide. All that does is hide the form; it doesn't close it. Instead, you need to use this.Close, which will close your original form.
If you want to make sure that your entire application exits, and in the process close any forms that may still be open, you can use the Application.Exit method anywhere in your form's code.
EDIT: To expand on my last comment, you might want something like this in your Program.cs file:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SignInForm frmSignIn = new SignInForm();
if (frmSignIn.ShowDialog() == DialogResult.Yes)
{
//If the sign-in completed successfully, show the main form
//(otherwise, the application will quit because the sign-in failed)
Application.Run(new ControlPanelForm());
}
}
}
Create a FormClosed event in control_panel form property window of control_panel and write the following line as
private void control_panel_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}

How to close a form and open a new form up?

I am wondering how do I close a form after I open up a new form.
For instance I have a login form and when they login I open a new form up but I want to close the login form down.
So how would I do this?
I am making these forms on windows mobile 6 compact edition. Not sure if it would be different then in windows forms.
Could do it like this:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (DialogResult.OK == new LoginForm().ShowDialog())
{
Application.Run(new MainForm());
}
}
}
Should ensure the LoginForm closes with DialogResult.OK if valid credentials and with DialogResult.Cancel otherwise:
private void ValidateLogin(...)
{
... // check credentials
if(validCredentials)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
... // up to you, maybe keep the form displayed to give user a chance to enter correct credentials
}
}
}
No need to hide anything.
If the main form is the Login window, you will only be able to hide it, since if you close it all other child windows will be closed too.
You have 2 options I guess:
1- Make your application's form, the main one. When the application starts, hide it and display the login window. When the login window is closed, show the main form.
2- Once the user enters his credentials in the login window, hide it (do not close it), and then open the other form. When the second one is closed, close both to shut the application down.
Most desktop apps implement login forms as modal dialogs. If you do the same thing, you would first display the main form, and then immediately display the login as a modal dialog, from the main form's form_loaded event. Once the login dialog has been closed, you can obtain login credentials from it and continue.
Just call Form.Close().
You will need to get an object for the login form that is open, or if you are in the login form when you want to close it, try calling this.Close()
I'm crazy, but I would suggest only using one form. Make the rest of your forms UserControls and just swap them in and out of the main form. Then you don't have to worry about any of this rubbish.
My Program.cs usually looks something like:
public class Program
{
static void Main()
{
MyApp application = new MyApp();
application.Initialise();
Application.Run(application.MainForm);
}
}
This way I can keep initialisation and logic outside of forms.

Categories