Top right x button doesn't completely close the program - c#

So, my project currently has two forms (Form1, Form2). When a user presses the login button Form1 is hidden and Form2 is shown. For some reason while running the program, if you press the X at the top right of Form2 it will close Form2 but keep Form1 running and hidden. How do I set it so that the X button will close both forms completely (so that you don't need to go back into Visual Studio to close it).

I assume your Form1 is the 'main' form, in other words, that's the form that's launched when you launch the application. So this would be your main thread, and Form2 is a form that's opened upon some event, and that form runs in a different thread.
So, when the Form2 is closed if you want the main program to exit as well (although this doesn't quite seem like a great design idea), then you'd have to use Application.Exit().
You can capture your form 'closing' event in your Form2 by adding the Form2_FormClosing event handler, and in it, you call the above method.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
}

Related

How to close Parent form without closing child form in Windows form in c#

I am trying to make one windows application which contains two forms. In form1(Parent) i created one button for open second form. On that button click event i want to close form1(parent) form and open form2(child) without closing form2, but when i am press that button both forms are closed so how can I do it?
Rather than using .Close() consider using .Hide() on your parent form, this shouldn't dispose of it but rather have it hidden.
Note that exiting out of your Child form means your application won't exit. To circumvent this you should consider subscribing to the OnFormClosed event to handle the form closure.
If you look in "Program.cs" there is a form to start with.
When this form is closed, the application is terminated.
enter image description here
It should be used as a way to hide the startup form.
Even if the child form is closed with the start form hidden, the program does not end.
As #Ae774 said, you need to handle it separately.
If you want to close form1(parent) form and open form2(child) form without closing form2 by click the button, you can refer to the following code:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
Hide();
f2.ShowDialog();
Close();
}
If you want to open the parent form after the child form is closed, you can refer to this code:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
Hide();
f2.ShowDialog();
Show();
}
Here is the test result

Using ALT+F4 to close all forms

I have 2 forms, form1 is the menu, with the buttons start, settings, quit, and form2 is where the program will run.
The problem I face is that if the user uses Alt+F4 on form2, it closes form2, but form1 runs in the background. I know I can use the form2 Closing event, so it can run an Environment.Exit(0), but that closing event also "activates" if I use the form2 "Back to Menu" button, which closes form2. I also tried just hiding form2 with the Menu button, but then when I need to call another form2, it opens up a new instance of it.
So, in summary: ALT+F4 should close the whole application, not just the current form, but can't use form2 Closing event, because I want to close form2 some other way too.
You can use KeyDownevent for that. Basically, you catch that key combination, tell the system that you are going to process it so it does not get passed to it and finally close the application. To close it, is always better to use Application.Exit() instead of Environment.Exit. You can see why here for example:
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.F4)
{
e.Handled = true;
//Close your app
Application.Exit();
}
}

Problems with loading ILPanel in ILNumerics

I have two forms in my code. When I click a button in Form1, it shows the second form (Form2). There is an ILPanel in Form2. The first time that I click the button, Form2 is shown without any problem, but if I close Form2 and then click the button on Form1 again, I get the following error message when Form2 is re-shown. Does anybody know why this is happening? Thank you.
The code is very simple but here it is again
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
}
This looks like a bug. One workaround - of course - is to reuse the form. "Closing" the form would not unload it, but only hide it. Clicking on button1 would only create the form for the first time and Show() it otherwise. That way, the OpenGL context (which seems to cause the problem) is not re-created every time you click on button1.
You can file a bugreport at http://ilnumerics.net/mantis

Order Break Between Model And Main Forms c#

I have two forms form1 is main form and form two is model form I want to set the forms as below:
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show(this);
}
The above would set the form1 owner of form2 and form2 would be shown but the problem is that this will break the order of forms on press of Alt+Tab keys hence I have tried it with another way as below.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
This would be works but the problem is that the dialogue forms will not allow me to maximise/minimise and close
My form2 is borderless form and it is set to show on specific location as to fit with main form1. My aim to do not shows the form2 in Alt+Tab list and as I close the form2 then form1 will show immediately without break order of form.
When I press Alt+Tab keys on first condition and try to close form2 then the other application shown instead of form1 which is I do not want.
Is there any solution of this problem?.
It really sounds like you could do the second form as a custom control.
See Microsoft's documentation and this set of examples.
Think of it as a standard control, like a Button, DataGridView, TextBox, or the like, except that you have total control over it. You can show or hide it, you don't have to worry about where it is positioned, it won't take focus away from the parent form, and so on. And you can put whatever other controls you want in it, encapsulate all their logic, etc.
A possible hack is to keep your parent form active after opening child form as a modal form, so that you could do maximize/minimize your parent as well. An extension method:
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool enable);
public static DialogResult ShowDialogSpecial(this Form formToBeShown, Form parent)
{
parent.BeginInvoke(new Action(() => EnableWindow(parent.Handle, true)));
formToBeShown.ShowDialog(parent);
return formToBeShown.DialogResult;
}
You can call:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//additionally do f2.ShowInTaskbar = false to make sense.
f2.ShowDialogSpecial(this);
}
This wont let child form truly act as non-modal form, since child form can cover over parent form.

Start new form on closing one. C#

When my program runs it closes form1 after a few seconds. Depending on what happened during form1's lifespan I may want to open form2. Normally I would just make a new instance of form2 and use the show() method. But form2 is then a child of form1 and then also closes. Does any body have an idea on how to get this to work? thanks.
For multi-form applications I tend to have one form that is the "main" form, which opens up the sub forms.
The main form is the one that gets started with Application.Run(...)
In your case you might want to have a blank form that can be the controller, and have Application.Run call that.
That form can then start instantiate your Form1 and run it.
e.g.
public ControlForm : public Form
{
Form1 form1;
Form2 form2;
public ControlForm()
{
form1 = new Form1();
form2 = new Form2();
}
public void Start() // or something similar
{
form1.ShowDialog(); // will block showing the form, or you can do other tricks
// to show the form here
if(form1.someFlag) form2.ShowDialog();
}
}
This is just "psudo-C#" code, but hopefully the concept makes sense
Then your main function can just run "ControlForm"
Its just a concept you might want to try
You can open a new form in your application's bootstrapper (main method). You will want to call Application.Run(yourFormHere). You would have two of these in a row in the order you want to show the forms. You could store the results of the first form in some static location and check that before showing the second form.
I ended up doing this:
Auth f = new Form1();
Application.Run(f);
if (f.authed)
{
Application.Run(new Form2());
}
I don't think that your problem is that the Form2 instance is a child form of the Form1 instance, but rather that the Form1 instance is your applications main form. That will make your application quit whenever Form1 closed. One way to prevent this is to alter the main method to not set Form1 as the main form (see here for details on that).

Categories