Closing current form and open another form
DetailForm df = new DetailForm();
df.Show();
this.Close();
But be careful, if you close the main form the application will be closed.
EDITED
To run this if the first form is the main form you need do more. Try something like this:
Change your Program.cs file to this:
public static class Program
{
public static bool OpenDetailFormOnClose { get; set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
OpenDetailFormOnClose = false;
Application.Run(new MainForm());
if (OpenDetailFormOnClose)
{
Application.Run(new DetailForm());
}
}
}
And in the main form, close it with this:
private void Foo(object sender, EventArgs e)
{
Program.OpenDetailFormOnClose = true;
this.Close();
}
If you set OpenDetailFormOnClose
with true after the close of the main form, the DetailForm will be call.
Close Current form open new one of that form closed
Declare the Variable for the Form2
as class variable outside of the scope of the btnSave_Click
method. This way you will be able to access it again when the button is clicked a second time.
Form frm2 = new Form2();
private void btnSave_Click(object sender, EventArgs e)
{
if(frm.Visible) // check whether the form is already showing
{
frm.Close(); // if yes close it first
}
frm2 = new Form2(); // then make a new form and show it
frm2.Show();
How to Close Current Form and Return to Main Form
I think you can simple pass your MainForm Object to FormA then FormA to FormB then on click of button you should simple show your FormA object.
As per your code, you are here showing new MainForm object which is not you have created first time as you are creating new object in buttonGoBack_Click event.
You should need to make change in FormA
public MainForm mainForm {get;set;}
public FormA(MainForm mainForm)
{
this.mainForm= mainForm;
}
You should need to make change in FormB
public MainForm mainForm {get;set;}
public FormB(MainForm mainForm)
{
this.mainForm= mainForm;
}
private void buttonOpenFormA_Click(object sender, EventArgs e)
{
formA displayformA = new formA(this);
displayformA.Show();
this.Hide();
}
private void buttonOpenFormB_Click(object sender, EventArgs e)
{
formB displayformB = new formB(this.mainForm);
displayformB.Show();
this.Hide();
}
private void buttonGoBack_Click(object sender, EventArgs e)
{
(this.mainform as MainForm).Show();
this.Close();
}
How to hide/close Current Form and opens a new form using a button inside the UserControl ? C#
This code creates a new instance and closes it hidden. However, this doesn't affect the main window as they are not related.
MainForm mainform = new MainForm();
mainform.Hide();
mainform.Close();
this.ParentForm.Hide();
can complete the operation of hiding the main window.
usercontrol:
code:
using System;
using System.Windows.Forms;
namespace WindowsFormsControlLibrary1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text == "admin" && this.textBox2.Text == "admin")
{
MessageBox.Show("Login Successfully");
UserForm dashboard = new UserForm();
this.ParentForm.Hide();
dashboard.ShowDialog();
this.ParentForm.Close();
}
else
{
MessageBox.Show("Login Error");
}
}
}
}
Mainform:
Output:
C# Close a form in panel and open a new form in the same panel
You can not create an instance of the MainForm
class and call its method, but you must use the existing instance. To use open forms, you must find the form with the name of the program.
Change Login_Click
to :
private void Login_Click(object sender, System.EventArgs e)
{
f1.ShowDialog();
this.Hide();
LoginForm loginForm = new LoginForm()
{
Dock = DockStyle.Fill,
TopLevel = false,
TopMost = true
};
MainForm mainForm = Application.OpenForms.OfType<MainForm>().Where(x => x.Name == "mainFormName").FirstOrDefault();
mainForm.showForm(loginForm);
}
In the line Where(x => x.Name == "mainFormName")
put the form name instead of mainFormName.
Related Topics
Deserializing JSON When Sometimes Array and Sometimes Object
How to Create a Custom Membership Provider for ASP.NET MVC 2
Finding the Default Application for Opening a Particular File Type on Windows
Switch Statement Fallthrough in C#
Wpf Webbrowser Control - How to Suppress Script Errors
Int.Parse, Input String Was Not in a Correct Format
Shorter Syntax for Casting from a List<X> to a List<Y>
Execute Multiple Command Lines with the Same Process Using .Net
Visual Studio 2010 Suddenly Can't See Namespace
How to "Steal" an Event Handler from One Control and Give It to Another
How to Get Difference Between Two Dates in Year/Month/Week/Day
How to Select Distinct Rows in a Datatable and Store into an Array
Opensubkey() Returns Null for a Registry Key That I Can See in Regedit.Exe
Multiple Cases in Switch Statement
Does the C# "Finally" Block Always Execute
Validate Image from File in C#
ASP.NET Calling Webmethod with Jquery Ajax "401 (Unauthorized)"