Hey there
I'm quite new at c#, nevertheless
today I tried to make a little application and I ran into a little problem.

Well, I got this form that i want to close when i click the button and then open a new one, not just hiding it.
first I tried this

this.hide();
new form3().Show();

Which worked fine, but the problem was that when i closed the program, form2 was still running in the background, which I don't want it to.

then I tried this

this.close();
new form3().Show();

but then I ran into another problem, when I clicked the button it would quickly open the next form and then exit the whole application.

So what should I do to close that form at the beginning so it doesn't have to run in the background?

Kind Regards
-Jazerix

Recommended Answers

All 17 Replies

If you MUST close the first form then you're doing it in the wrong order.

Instead of using:

this.close();
new form3().Show();

Try using:

new form3().Show();
this.close();

If you close the only active form prior to opening a new form you get the inadvertant side-effect of terminating the application in most cases.

Hope this helps :) Please remember to mark the thread solved once your issue is resolved.

Take a look at the tutorial i wrote. It shows how to show/hide current form when opening a new one. The text portion also outlines how you can close the first form instead of showing it.

When you open a new form it gets added to the Application.OpenForms collection which keeps it alive even if the form that creted it is disposed of (not very tidy in my opinion, but it works). The problem occurs when the form you close is the applications main form (the one called in your Program.cs file as Application.Run(new Form1()); ). If you close the main form the application loop will exit and your program will close. The code i show in my tutorial can be used with ANY form, even the main application form :)

commented: Thanks for your help +0

If you want to go all out, you should inherit the ApplicationContext and create your own forms manager.

:)

I'm quite new at c#

I think that may be a little out of their depth ;)

If you MUST close the first form then you're doing it in the wrong order.

Instead of using:

this.close();
new form3().Show();

Try using:

new form3().Show();
this.close();

If you close the only active form prior to opening a new form you get the inadvertant side-effect of terminating the application in most cases.

Hope this helps :) Please remember to mark the thread solved once your issue is resolved.

i just tried that and im getting the same error, it closes the whole application :<

Take a look at the tutorial i wrote. It shows how to show/hide current form when opening a new one. The text portion also outlines how you can close the first form instead of showing it.

When you open a new form it gets added to the Application.OpenForms collection which keeps it alive even if the form that creted it is disposed of (not very tidy in my opinion, but it works). The problem occurs when the form you close is the applications main form (the one called in your Program.cs file as Application.Run(new Form1()); ). If you close the main form the application loop will exit and your program will close. The code i show in my tutorial can be used with ANY form, even the main application form

couldnt understand most of it :(, sorry about the inconvience but isnt there like, just one simple way that it can go to the next form and closing the last one?
By the way, "Form2" is my startup application if that matters.

Also i was kinda thinking, if you could edit the close button so it would send "Application.Exit();"

The weird thing about this,I use the form.close(); everywhere else in my app, but it only seems to be this form that buggs :(

Nevertheless, thanks for all your help, really appreciate and sorry for being such a newb

It certainly does matter. When you close Form2 you close your application.
Did you have a look at the code? I realise that the text at the top can be hard to follow if you aren't familiar with the parent - child form paradigm.
The code is fully commented and shouldn't hold much you haven't seen before.
It creates a new instance of a form, attaches an event handler to its close event, then hides the current form when it is shown. Apart from the event handler it is doing the same as you already do so I'm gonna assume its the event handler thats causing the confusion.

Event handlers are blocks of code which run when a certain action occurs (such as a button being pressed or a form opening). What my code does is create a block of code which will be run when the new form is closed. Inside that code block i have "this.Show()" which will make the old form reappear. If you replace it with "this.Close()" the old form will be closed at the same time as the new form. Have a read through my Event Handler Tutorial for more info :)

EDIT: Thats right nick...another tutorial ;)

This is very related to my last post.

When you look at program.cs you will see that the last line is Application.Run(new Form1()); This sets up what's known as the "ApplicationContext" and sets your form as the application parent and shows it on screen.
In order to exit the application, all you need to do is close that form, this is great in terms of management and making sure your application isn't sat there doing nothing after you've closed all the forms (meaning you cannot exit)

Unfortunately this has the added side effect that closing the form, regardless of where and how you do it in your application, will close the entire application.

There are only two ways around this.

1. Only hide the form. This doesn't close it and hence your application will stay running. The best way of doing this is to show your second form as a dialog.

{
   /* ...Code Here... */
   Form3 myForm = new Form3();
   this.Hide(); // The main form can no longer be seen
   myForm.ShowDialog(); // Shows the form as a dialog and PAUSES execution of this method. The next line will not be executed until "myForm" is closed
   MessageBox.Show("This is a message!");
}

That's the simplest way.

2. Create your own ApplicationContext and run that.

Having your own ApplicationContext is great for controlling your application and when it exits and what it does. If you wanted a tray icon application, you would need to have your own ApplicationContext.

Creating and using an ApplicationContext is a lot more complex. Setting one up is shown below. The only method you need to be worried about is the "ExitApplication" method.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

namespace MyApplication
{
   public class MyApplicationContext : ApplicationContext
   {
      private IContainer _components;
      private NotifyIcon _trayIcon;
      private ContextMenu _trayMenu;

      public void ExitApplication(Object sender, EventArgs e)
      {
         ExitThread(); // Causes the thread that the application runs in to exit
      }
   }
}

Obviously in addition to this you would need to manage how to open and close your forms and keep track of everything. So I wouldn't try using the ApplicationContext unless you're confident you can do all that :)

commented: Thank you +0

thank you, ill give it all a try as soon as i get home :D

Say what, I got it working! :D
In case anybody is wondering, here is the code (thank you Ryshad)

private void button1_Click(object sender, EventArgs e)
        {
            Form3 child = new Form3(); //create new isntance of form
            child.FormClosed += new FormClosedEventHandler(child_FormClosed); //add handler to catch when child form is closed
            child.Show(); //show child
            this.Hide(); //hide parent
        }
        void child_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }

Thank you all so much!

Last Question, is there any way I can edit the properties for the close button (X)?

Kind Regards
-Jazerix

Which property did you want to change?

Well, just the property of the Close button, like instead of the close button closing the window it should send "Application.Exit();" instead.

Hook into the FormClosing event. There's no way to directly modify what the X button does.

alright, thanks for all the answers, how do i mark my thread as solved?

Below the reply text box it should have a link for solving the thread :)

alright im now stuck with another problem :P
i got my form3 to open a new form (form4) and make it the default form (child, not sure what it's called)
nevertheless my problem is, I want form3 to show up and make it the default (^.-) form again when i press the close button (X) of form4 instead of adding a back button.
Hope you get what i mean

Edit: just talked to a friend of mine, and he told me that he didn't think it's possible to add a scrollbar to a groupbox, is that true?
Kind Regards
-Jazerix

If you go back and check the code in the tutorial i linked the code shows how to hide the current form then recall it when the child form closes. Basically, you have this.Show() in the Form3_Closed event handler.

And no, to the best of my knowledge a group box doesn't show a scroll bar, but a panel control is very similar and does have scroll bars.

commented: Awesome Help rep :D +1

oh yeah, sorry forgot completely about that
once again, thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.