0

Possible Duplicate:
C# Working with mutliple Forms

I have two forms in C#. I want to close one form and show other. The code is as follows:

AMBR A = new AMBR();
this.Close();
A.Show();  

The current form is my main form. It shows my second form, but than closes both and my program stops. I know another standard approach is to hide the form, but my main form only has a logo and loading bar. It don't need any interaction with the user. When I hide it, after the second form is closed the program remain open (as seen in the task manager) and continues to occupy resources. I want the main form to close and second form to remain open.

Community
  • 1
  • 1
user1366440
  • 345
  • 2
  • 9
  • 19
  • 4
    Why not make the second form the main form and show the first as a splash screen? – King Jul 17 '12 at 17:56

6 Answers6

3

Open your "Program.cs". Modify the code to be as follows, where SplashFrm is the current Form that is being created in your Application.Run call

static class Program
{
    private static EventHandler idleTemp;
    private static SplashFrm splash;


    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        splash = new SplashFrm();
        splash.Show();
        idleTemp = new EventHandler(Application_Idle);
        Application.Idle += idleTemp;

        Application.Run(new AMBR());
    }

    static void Application_Idle(object sender, EventArgs e)
    {
        splash.Close();
        Application.Idle -= idleTemp;
        idleTemp = null;
        splash = null;
    }
}

Then, after AMBR is successfully loaded, call Application.RaiseIdle(null); and your splash will be closed and cleaned up.

Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
2

No you cannot close the main form and maintain the second form opened. But you can hide it.

this.Hide();

EDIT:

Another solution could be use the second form as the main form and make it invisible while the second form (splash form) is opened.

So:

this.Visible = false;

//Show the second form
Omar
  • 16,329
  • 10
  • 48
  • 66
  • 3
    OP discusses why this isn't a desirable solution. *"I know the other method is to hide the form but my main form has only logo and loading bar it don't need any interaction with user so when i hide it when second form is closed the program remain opened in the task manager and occupy resources"* – Servy Jul 17 '12 at 17:59
  • 1
    @Servy - The fact the author does not know how to implement this suggestion the CORRECT way, doesn't mean it should be downvoted, using `this.Hide()` IS the correct answer. The solution to the author's problem is to `Close()` the hidden form when the `AMBR` form is being closed. In other words `Application.Exit()` – Security Hound Jul 17 '12 at 18:08
  • 1
    @Ramhound Not only is using this.Hide() not the correct answer, it's not even a good idea. Sure it will work, but it's a bad idea as you have this object floating around for no reason. Granted, based on the question, performance and memory management is probably the least of concerns, but there's no reason to send him down the wrong path. – Jaime Torres Jul 17 '12 at 18:17
  • 1
    @Ramhound Using `this.Hide` clearly is not the answer. The OP has tried it, and it doesn't work. If using `this.Hide` in addition to a number of other things works, then that might be an answer. Since all of those other things aren't included, or even eluded to, no; this is no an answer. Additionally, as J. Torres has said, it is not "correct" to have your splash screen be a main form that stays around hidden for the entirety of the application and to use `app.Exit` to end the application. It's just poor design that may or may not work, but certainly doesn't address the actual problems. – Servy Jul 17 '12 at 18:59
2

There is an example on the Application.Run MSDN page that explains how you can inherit from ApplicationContext to make your program end only when the last form has been closed, and not just once the main form is closed.

Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
1

Have you tried this.Hide(); instead of this.Close();?

Mike Atlas
  • 8,193
  • 4
  • 46
  • 62
DROP TABLE users
  • 1,955
  • 14
  • 26
  • 3
    OP discusses why this isn't a desirable solution. – Servy Jul 17 '12 at 17:58
  • Yes, but your program can't keep executing if you close the main thread. So using this.Close(); wont wont work here if it's his main form. – DROP TABLE users Jul 17 '12 at 18:09
  • 1
    @Servy - Doesn't mean this isn't the correct solution, if left to his own, the author would implement solution. `Application.Exit()` solves all. – Security Hound Jul 17 '12 at 18:09
  • @Ramhound Well, no, it doesn't solve it all. It solves one piece of the puzzle. it's certainly *possible* to pursue this line, but that would need to be detailed in the answer for it to be appropriate. Clearly the OP has tried just hiding the form and it didn't work. If you want to propose that as the answer you will need to provide everything needed to actually make it work. Additionally, the problems are a result of a root design problem. Rather than hacking around that problem it's easier to just fix the root issue (of not having the right main form). – Servy Jul 17 '12 at 18:56
  • Yes Tried This But Its In Accurate – user1366440 Jul 19 '12 at 07:45
0

The problem probably happens because your main method is creating the first form and waits for it to close. when it closes the main proceeds and reaches the end which result in your application shutdown.

either make the main method create the second form or dont close the first one and just hide it + hide the taskbar icon.

  • This easily could be a comment, how about some code, like everyone else? – Security Hound Jul 17 '12 at 18:09
  • 1
    @Ramhound Only 1 of 5 answers actually has any real amount of code in it, so I don't see how "everyone else" uses code. Additionally explaining a problem without code is perfectly acceptable as an answer to most questions, if explained well. – Servy Jul 17 '12 at 19:02
  • But the loading bar should appear first than the next form that's my design rather than form first and loading bar afterward that's not what i want that's poor – user1366440 Jul 19 '12 at 07:47
0

It sounds like you have the wrong main form. You should change the other form that you are opening to be your main form, and the form that is currently your main form should be opened by the other form. When your current main form is closed, have it Show your new main form. Have your new main form hidden when the program starts.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • But the loading bar should appear first than the next form that's my design rather than form first and loading bar afterward that's not what i want that's poor – user1366440 Jul 19 '12 at 07:47
  • @user1366440 As I said in my answer, when your new main form starts have it hide itself and show the other form, it's an easy fix and a fairly standard practice. – Servy Jul 19 '12 at 13:46