Close All application click X Winform - c#

I'm trying to change the names of the datagrid and labels in Form2 from Form1 based on the selection.
private void button1_Click_1(object sender, EventArgs e)
{
this.Hide();
Form2 frm = new Form2();
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.dataGridView1.Columns["FirstName"].HeaderText = "Prenom";
frm.dataGridView1.Columns["LastName"].HeaderText = "Nom";
this.Hide();
frm.Show();
}
The above approach is working fine for me but I have a problem. When I click on X in the second/Form 2 it's just closing the Form2 not the Form1. How can close all the application when i click on the X.
Is there any better way of doing this??? The reason why i'm not using I'm using Telerik and I don't find any option to add resource file in that. Please correct me if i'm wrong. Thank you.

You simply need to attach an event handler to the Closed event of the newly created form so that it closes the main form when it's closed:
private void button1_Click_1(object sender, EventArgs e)
{
this.Hide();
Form2 frm = new Form2();
frm.FormClosed += (_, args) => this.Close(); //Added this method
frm.Show();
}
Add that same method to the other click handler as well.

The easiest way to close an application is to call the static method: Application.Exit
Another method if you only want to close one single form would be to add a handler for the FormClosing event and close the main form there.

Related

Maximizing the main form when you close one

I have this project for school which requires me to use 2 forms, but I can't have both of them open at the same time.
This means when I open one form, the other one minimizes. When you close this one, I'd like to make it so that the minimized one is restored.
This is what I have now for minimizing it:
private void Mkbtn1_Click(object sender, EventArgs e)
{
var newForm = new form1();
newForm.Show();
this.WindowState = FormWindowState.Minimized;
}
In form1, which is opening the form2 do things like this
`private void button1_Click(object sender, EventArgs e)
{
Form2 newofrm = new Form2();
newofrm.parentForm = this;
newofrm.Show();
this.Hide();
}`
And then in form2 use this
`private void Form2_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized && parentForm != null)
{
// Do some stuff
parentForm.Show();
}
}`
Here parentForm is public member of form2 of type Form, also if you want this do be done when form is closed then add form closing event and add same code in the event handler

C# when opening new form, old one has to stop

I have new form opening when button is clicked, like this:
private void button8_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
//more code
}
Does this mean that, when i click Button8, form2 will be opened, but code after form2.ShowDialog() will not be executed until i close form2?
If this is not the way how to do that, please, can you tell me.
Use Show(); not ShowDialog(); That should works.

How to instantly call action in Form1 on closing Form2

So basically i have Form1
From Form1 I can open Form2 with this code:
private void btn_Komplexity_Click(object sender, EventArgs e)
{
Form2 kompleksaForma = new Form2();
kompleksaForma.ShowDialog();
}
When Form2 is opened there is something and at the end there is this.Close();
After this.Close(); (closing Form2) is it possible to call instant action on Form1?
If you're sticking to ShowDialog(), this function will block until the form is closed.
private void btn_Komplexity_Click(object sender, EventArgs e)
{
using (Form2 kompleksaForma = new Form2())
{
kompleksaForma.ShowDialog();
PutStuffHereAfterClose(); // (or outside the using block if it doesn't need
// to access properties of kompleksaForma)
}
}
If you're showing Form2 as a modal window using form2.ShowDialog() or form2.ShowDialog(this), then...
form2.ShowDialog(this);
if (form2.DialogResult == DialogResult.OK)
{
CallOtherStuffHere();
}
... as the ShowDialog() method will block execution until the closure of Form2, then continue.
I'm using DialogResult above to test for validity, but you could implement some other method, if you wish.
If you're showing Form2 as a non modal window, then you should pass a reference of Form1 to Form2 first. This could be done in its constructor...
var form2 = new Form2(form1);
Or, you can pass it in the Show() method, to set form1 as its parent...
var form2 = new Form2();
form2.Show(form1);
Then, you can access the parent form via form2.Parent. However, you may have to cast it to a Form1 instance before you call your methods explicitly. And this can be done in the Closing event handler of Form2.
Further info here regarding modal and modeless windows:
https://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx
There are some events triggered after the window is closed. You can subscribe to them and add your code to the handler method:
Form2 kompleksaForma = new Form2();
kompleksaForma.FormClosing += KompleksaForma_FormClosing;
kompleksaForma.FormClosed += KompleksaForma_FormClosed;
kompleksaForma.Deactivate += KompleksaForma_Deactivate;
kompleksaForma.ShowDialog();
And then implement one of the handlers like that:
private void KompleksaForma_FormClosing(object sender, FormClosingEventArgs e)
{
// Your code here
}
private void KompleksaForma_FormClosed(object sender, FormClosedEventArgs e)
{
// or here
}
private void KompleksaForma_Deactivate(object sender, EventArgs e)
{
// or here
}
First will trigger FormClosing, then FormClosed. Last one is Deactivate.

Hiding Form 1 and Show form 2 on Windows Form 1 Load

Am a newbie when it come to C# Programming. Here's my trouble:
I want to Show a new Form 2 and Hide Form 1 on the Windows Form 1 Load.
Here's my current codes;
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
this.Hide();
}
My en-counted Error with the current Code:
When Form 1 load its loading Form 2 but it's not hiding itself. this.Hide Statement not working, I've try this.Close but this will Close the entire software as it's closing the main form.
Can anyone kindly help me with this error.
The Error i thought is that you are showing the form2 before hiding the previous form.
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
form1.Visible=false;
Form2.Show();
}
You can also make form2 as modal by using show dailog method() by which focus is given to the form2 and form1 becomes inactive though it will be shown.
modal and modeless forms https://msdn.microsoft.com/en-IN/library/aa984358%28v=vs.71%29.aspx
Here's how I manage to make this work.
New Codes:
Form 1
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
}
private void Timer_Tick(object sender, EventArgs e)
{
if (Properties.Settings.Default.Status == "Form2Visible")
{
this.Hide();
}
}
Explanation:
On the Windows Form 1 Load am showing the the Form 2, then using a timer to verify a Properties.Settings Value. If the Properties.Settings = Form2Visible, Form 1 will Hide. Once am done on Form 2 I simply need to change the Properties.Settings to something else and stop the Timer on Form 1.
If their is a simplest way let me know.
You can use Visible property to hide your form.
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
this.Visible=false;
}

Open an existing form from the main form

I designed two forms: Form1 and Form2. Form1 is the main form. There is a button in Form1, if I click the button, then Form2 will pop out. I want to do something on Form2.
// click button in Form1.
private void button1_Click(object sender, EventArgs e)
{
Form form2= new Form();
form2.ShowDialog();
}
But Form2 is a new form rather than an existing form.
It is wrong.
How? Thanks.
You are creating instance of Form class not the Form2 which you have in your project. Create instance of Form2 which you created earlier and then call ShowDialog in it.
You might have notice the in the program.cs something like Application.Run(new Form1()); Here we create the instance of Form1 and pass to Run method.
Do it this way by creating instance of Form2 and calling ShowDialog() method to show it
Form2 form2= new Form2();
form2.ShowDialog();
You create blank form with
Form Form2= new Form();
You should use
Form2 form2= new Form2();
Complete code:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2= new Form2();
form2.ShowDialog();
}
Declare
Form2 form2= new Form2();
like your class member and use it like this:
private void button1_Click(object sender, EventArgs e)
{
form2.ShowDialog(); //blocking call
//or form2.Show() //non blocking call
}
EDIT
Based on correct comments, to make this work instead of executing the Close() on the function which will lead to Dispose() you need to use form2.Hide() to make is simply invisible
private void button1_Click(object sender, EventArgs e)
{
InputForm form1 = new InputForm();
form1.Show();
}
Here InputForm means which form you want to open.
The question is "Open an existing form from the main form"
Okay lets change it a little, Open an existing instance of form from the main form.
when you show a form
new Form2().Show();
lets say you hid it using
Form2.Hide();
you guys can use this
var Form2_instance = Application.OpenForms.OfType<Form2>().Single();
Form2_instance.Show();

Categories