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
}
Related
So I want to show a form and close the other form so I did this in form1:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
x.Show();
this.Hide();
//didn't use this.close() cause then the whole program will close instead of
//just the current form
}
Then I wanted the main form to open again after the 2nd form is closed so I did this in form2:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 x = new Form1();
x.Show();
}
Now the problem is when I'm back in the main form after closing the 2nd form. if I close the main form it doesn't fully close and still remains open in the background (I found it in task manager). I think its because the "show" method just opens another form instead of making the form appear so the main form which got hidden is still there running in the background.
what should I do to make the form get closed when I exit it?
I tried putting this.close(); in the form closed and form closing event but both resulted in a crash.
When you write:
Form1 x = new Form1();
you are creating a new Form1 object, so x refers to the new one and not to the original one. Instead, you could use this:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var form2 = new Form2())
{
this.Hide();
form2.ShowDialog();
}
this.Show();
}
When ShowDialog() is called, the code following it is not executed until after the dialog box is closed, so this.Show() will execute only after Form2 is closed.
Another option is to simply subscribe to the FormClosed() event of Form2 when you create it, then un-hide your instance of Form1 from there:
// ... all in Form1 ...
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form2 x = new Form2();
x.FormClosed += X_FormClosed;
x.Show();
}
private void X_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show();
}
So then when you close Form2 your instance of Form1 will automatically re-appear.
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.
When I click on About, a new form opens and the main form hides. I have a close button on the About button, that closes the About form, and it shows up the Main form. However, when I close the main form with the 'X' close button on top, the Main form closes, but the process is still running in the background. Everything is okay when I dont click on the About button. It kills the process.
In your Form2, you need to bring up the existing Form1 instance rather than creating a new one (that's why closing this newly created Form1 won't exit the application, as your old Form1 instance is still hiding...).
So you could get reference passed when creating aboutdialog, and show that instead when closing aboutdialog:
Form1:
private void AboutButton_Click(object sender, EventArgs e)
{
var aboutdialog = new Form2(this);
aboutdialog.Show();
this.Hide();
}
Form2:
Form1 _parentform;
public Form2(Form1 parent)
{
_parentform=parent;
InitializeComponent();
}
private void CloseButton_Click(object sender, EventArgs e)
{
_parentform.Show();
this.Close();
}
In your "AboutButton_Click" method you hide the Form1. But when closing the About dialog, you do not show back the Form1, but create a new one. The original Form1 stays hidden and prevents your application from closing.
better to use showdialog.
private void AboutButton_Click(object sender, EventArgs e)
{
var aboutdialog = new Form2();
aboutdialog.ShowDialog();
}
When form2 is closed a new instance of form1 is opened, instead of the original one. You could include a reference to Form1 inside of Form2, or use the default Ownerof a form, by showing the dialog with Form1 as its owner:
aboutdialog.Show(this);
in the about...click
and closing
private void CloseButton_Click(object sender, EventArgs e)
{
Owner.Show();
this.Close();
}
I'm working on project with sign in feature
When I run the project there is a form (form1) run the sign in .
after i click on login button build another form (form2) - It's the form of my program .
and made the first form (form1) hide .
The problem is when I press at the X button in form2 it's close but the form1 it's still running .
I tried to close the form1 instead of hide ... but this will close form2 before launching
In form1:
this.Hide();
Form2 x = new Form2();
x.Show();
I think you have your forms around the wrong way.
Form1 sould be your app and shold show Form2 as a dialog when it first loads, then when it closes you can process the result and decide wether to continue or close the application.
Something like:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
Form2 myDialog = new Form2();
if (myDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
// failed login
// exit application
}
// all good, continue
}
}
You could subscribe to the child forms FormClosed event and use that to call Close on the parent form.
x.FormClosed += new FormClosedEventHandler(x_FormClosed);
void x_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
try this, in the log in button if access is granted
private void logInBtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
this.Hide();
}
then in form2 if you want to exit
private void exitBtn_Click(object sender, EventArgs e)
{
Application.Exit();
}
hope this helps.
You go to your form2 then in the event of the form look for FormClosed.
Put this code in your eventhandler:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
FormClosed is the event whenever the user closes the form. So when you close the form put a code that will exit your application that is -applicationn.exit();-
Hope this will work.
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.