how can i dispose the first form when calling second form using c#, Here is my code, :
Form2 f2 = new Form2();
f2.Show();
this.Dispose();
how can i dispose the first form when calling second form using c#, Here is my code, :
Form2 f2 = new Form2();
f2.Show();
this.Dispose();
When you run the command new Form2()
you are creating a new Form instance from Form1
as a child element. Therefore, you cannot dispose the parent, Form1
(which is this
in your code).
Application will close if you dispose the main form.
Try this code
this.Visible = false;
Form2 f2 = new Form2();
f2.ShowDialog();
this.Visible = true;
This will just hide the main form as long as Form2 is open.
Why you are trying to dispose the main form, Instead You can simply hide the first form:
Form2 f2 = new Form2();
f2.Show(); //It shows the new form(Second Form)
this.Hide(); //It hides the current form
It will hide the Main form and shows the Second form.