How can I restore a form? - c#

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.

Related

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
}

Close Main Form

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();

C# application exit, when close not the main form with x

I have an application which have 2 forms, a main and an other form.
I have an exit button in the other form (when pressed Application.Exit()) but how can i call this, when somebody click the x on this form?
Call it on form closing or form closed event of your form.
In OtherForm.Closed(sender, e) or OtherForm.Closing(sender, e)
Application.Exit();
Simple; in Form1.cs
private void CloseApplicationButton_Click(object sender, EventArgs e)
{
Form2 m= new Form2();
m.X_Click(this, null);
}
so in form2.cs make click event public
public void X_Click(object sender, EventArgs e)
{
Application.Exit();
}
Why not just have both do
Application.Exit();
Unless you have some common code you want to run as part of the exiting scenario?

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.

Printform in c# 2008

How to use the powerpack PrintForm? i want to print the winform.
First use the name space
using Microsoft.VisualBasic.PowerPacks.Printing;
Suppose you have two forms say Form1 and Form2. You have a print button in Form1 and on click the button you need to print Form2. Foe this do as
Form 1
private void printButton_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
Form 2
private void Form2_Load(object sender, EventArgs e)
{
this.Hide();
printForm1.Print(this, PrintForm.PrintOption.ClientAreaOnly);
this.Close();
}

Categories

Resources