Close Main Form - c#

I am developing a simple app using c# Windows forms. The main form open another form, but i dont want the both forms. I want it that when the second form opens the first form closes. Since the first form is the main using
this.Close();
after showing the second form will close the both. So i used this instead
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
private void frmData_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
I want to know if there is any other way to do this.
Any help will be appreciated.

Do not pass your main form as argument to Application.Run:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm frmMain = new MainForm();
frmMain.Show();
Application.Run();
Thus you will be able to close it when showing another form:
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.Close();
}
To close application you should use
Application.Exit();

Hide the first form.
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.Hide();
}
private void frmData_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}

Actually there's no possible way to close the main form if you close the main form it closes all forms of the same project. You can only hide it.
BTW you can open and show a form in only one simple line like this: new FormName().Show();

Related

Close Form when not clicking on it

I want to know if there is any event when you click on the rest of the screen and not the Windows Form the Form closes. Is it because of the ShowDialog()?
My Main Form is just with a notifyIcon and when I click it I call Form1.ShowDialog();
Main.cs:
private void ShowForm1(object sender, EventArgs e)
{
form1.Left = Cursor.Position.X;
form1.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form1.Height;
form1.ShowDialog();
}
Form1.cs:
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Test";
}
You need to run the dialog box non-modally, not modally. Think about it, when you run it modally, the dialog box takes over the UI and plays games with mouse-clicks elsewhere, preventing you from running. You don't want it to be modal anyway.
I created a simple Windows form with a button that includes this handler to open a small AutoCloseDialog form I created (and populated with a few controls):
private void button1_Click(object sender, EventArgs e)
{
var dlg = new AutoCloseDialog();
dlg.Show(this); //NOTE: Show(), not ShowDialog()
}
Then, in the AutoCloseDialog form, I wired up the Deactivate event. I did it in the designer (where this code is generated):
this.Deactivate += new System.EventHandler(this.AutoCloseDialog_Deactivate);
Finally, here is the handler for the Deactivate event.
private void AutoCloseDialog_Deactivate(object sender, EventArgs e)
{
this.Close();
}
I think this does what you are asking.
Yes, you can create a similar function like this, which closes the Form if the Form lost focus (in Form1.cs)
public void Form_LostFocus(object sender, EventArgs e)
{
Close();
}
and then you add the LoseFocus EventHandler (in Form1.Designer.cs):
this.LostFocus += new EventHandler(Form_LostFocus);

Opening a Form from another Form

I have no idea why this isn't working. Here's the code
private void button1_Click(object sender, EventArgs e)
{
this.Close();
new MainScreen().Show();
}
All that this is doing is closing the current form, it isn't opening the MainScreen Form. What am I doing wrong? I've used this same code in another project.
This code worked in my program:
this.Hide();
this.Close();
MainScreen mainScreen = new MainScreen();
MainScreen.ShowDialog();
Open the other form first. Then Hide current one:
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
new MainScreen().Show();
}

How can I restore a form?

I'm working on a Form application which is written in C#. How can I hide a form and then restore it?
Form 1:
private void PlayButton_Click(object sender, EventArgs e)
{
this.Hide();
}
Form 2:
private void BackButton_Click(object sender, EventArgs e)
{
this.Close();
//code which shows again form1
}
You will need to have a reference to the first form (Form1 in your example) so that you can call the Show() method against it.
A simple approach might be to have a property on Form2 that you assign with a reference to the first form.

Close existing form on opening another form

I am working in compact framework 3.5,i am working on windows mobile application.
form1.cs
internal void menuItem_Click(object sender, EventArgs e)
{
this.Close();
form2 obj = new form2();
obj.ShowDialog();
/////other code
}
form2.cs
public void backbutton_Click(object sender, EventArgs e)
{
form1 obj = new form1();
obj.ShowDialog();
/////other code
}
Initially i am in form1 and i am navigating to form2.In form2,a back button is there and on clicking backbutton i need to show the from1.Now i can show the form1 on backbutton click,but i need to close the previous opened form1.
I tried with this.Close() and this.Dispose(). This makes me exist from my application.
How to do this in compact framework.
Best way to work with that kind of things is using a singleton. When you show your first form, you instance the first form, then you show the second creating it instance and hiding the first one (use hide(), not close(), then your instance will still on memory). On back button click, you only have to hide() the second, and show() the first.
I would recommend instead of creating new form1 from form2, just close form2 and go back to form1:
form2.cs:
public void backbutton_Click(object sender, EventArgs e)
{
this.Close();
}
form1.cs:
internal void menuItem_Click(object sender, EventArgs e)
{
form2 obj = new form2();
obj.ShowDialog();
/////other code
}

Opening a new form, and closing the form that called it

I have a button on the first form, that when clicked opens a second form and closes the first.
However, no mater what I do 'both' forms close.
This is the button on the first form:
private void btnReports_Click(object sender, EventArgs e)
{
Form f2 = new frmForm2();
f2.Show();
}
And this is the code in the Load event of the second form
private void frmReports_Load(object sender, EventArgs e)
{
Application.OpenForms["frmForm1"].Close();
}
I also tried
private void btnReports_Click(object sender, EventArgs e)
{
Form f2 = new frmForm2();
f2.Show();
this.Close();
}
You probably have this as startup:
Application.Run(new frmForm1());
If frmForm1 is closed then the application stops. You should hide the form using frmForm1.hide();
Take a look at the following Application.Run(ApplicationContext) method. There is an example on how to set up your application so that it will only exit when the last form is closed, not when the main form is closed.

Categories

Resources