none
unload form

    Question

  • i have two forms where one is a login form (form1)and the another is form 2. when click the login button, it opens the form2 . The problem is that when the form2 open, the form 1 doesn't close. i tried using this.dispose(false) but it close the whole application.What goes wrong to the code?

    //in form1
     private void loginControl1_LoginClicked()
            {
                                   
                    Form2 f = new Form2();
                    f.ShowDialog();

                    this.Dispose(false);  <--can't unload form1 but it close the whole application when     click close button in form2
            }

     private void loginControl1_CloseClicked()
     {
                this.Close();
     }


    //in form2
    private void btnclose_Click(object sender, EventArgs e)
     {
                this.Close();
     }

     

     

    Thursday, August 26, 2010 2:17 AM

Answers

  • Hi htmlforums,

     

    Welcome to MSDN Forum!

     

    There’re two methods to help you to code the program as you said.

     

    1)

    private void button1_Click(object sender, EventArgs e)

    {

      this.Close();

      System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));

      t.Start();

    }

     public static void ThreadProc()

     {

      Application.Run(new Form2());

     }

    =============================================

    2)

    Replace the Application.Run(new Form1()) with:

     

    var form = new Form1();
    form.Show();
    Application.Run();

     

    The Click Event was doing:

     

    var newForm = new Form2();
    this.Hide();
    newForm.Show();
    this.Close();

     

    BUT: You are responsible to End the Application! Closing the Windows will no longer stop the Application from running. So you have to call the Exit or ExitThread Function to end the application!

     

    And if you want to know more about this topic you can go to the link following. There’re any reply has talk about this skill.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/090bf80f-85b3-4952-99d6-9000d792529f

     

    And the solution from Rod Stephens is also a good one to give you the expect result.

     

    If there’s anything unclear please feel free to let me know, and I am glad to help you.

     

    Have a nice day!

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    Monday, August 30, 2010 1:19 PM
    Moderator

All replies

  • Try using .Show instead of .ShowDialog. ShowDialog displays a modal form from a "main" form. When the modal dialog is open, you cannot close the dialog that opened it.

    You can try showing it using Show instead.

    Then instead of calling .Dispose, you should be able to call Close.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Thursday, August 26, 2010 3:15 AM
  • Normally when you close the startup form, the entire application closes including any other forms that you have displayed.

    I would turn the whole thing inside out. Make Form2 the startup form. Make its Load event handler display the login form modally using ShowDialog. If the user logins in correctly, display Form2 as normal. If the user doesn't enter a valid username and password, make the Load event handler close Form2 so the whole thing disappears.

    When you're done, Form2 is running and it's the startup form so it will close the application as you would expect.

    Rod

    C# 24-Hour Trainer
    http://www.csharphelper.com/24hour.html

    Thursday, August 26, 2010 3:47 AM
  • Try using .Show instead of .ShowDialog. ShowDialog displays a modal form from a "main" form. When the modal dialog is open, you cannot close the dialog that opened it.

    You can try showing it using Show instead.

    Then instead of calling .Dispose, you should be able to call Close.

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    i using .Show() , follow by this.close(),  it close the whole application.
    Thursday, August 26, 2010 4:39 AM
  • Normally when you close the startup form, the entire application closes including any other forms that you have displayed.

    I would turn the whole thing inside out. Make Form2 the startup form. Make its Load event handler display the login form modally using ShowDialog. If the user logins in correctly, display Form2 as normal. If the user doesn't enter a valid username and password, make the Load event handler close Form2 so the whole thing disappears.

    When you're done, Form2 is running and it's the startup form so it will close the application as you would expect.

    Rod

    C# 24-Hour Trainer
    http://www.csharphelper.com/24hour.html

    do u have any sample code for this?
    Thursday, August 26, 2010 4:43 AM
  • Here's an example:

        http://blog.csharphelper.com/2010/08/26/display-a-simple-password-dialog-before-a-program-starts-in-c.aspx

     

    Rod

    C# 24-Hour Trainer
    http://www.csharphelper.com/24hour.html

    Thursday, August 26, 2010 1:54 PM
  • Hi htmlforums,

     

    Welcome to MSDN Forum!

     

    There’re two methods to help you to code the program as you said.

     

    1)

    private void button1_Click(object sender, EventArgs e)

    {

      this.Close();

      System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));

      t.Start();

    }

     public static void ThreadProc()

     {

      Application.Run(new Form2());

     }

    =============================================

    2)

    Replace the Application.Run(new Form1()) with:

     

    var form = new Form1();
    form.Show();
    Application.Run();

     

    The Click Event was doing:

     

    var newForm = new Form2();
    this.Hide();
    newForm.Show();
    this.Close();

     

    BUT: You are responsible to End the Application! Closing the Windows will no longer stop the Application from running. So you have to call the Exit or ExitThread Function to end the application!

     

    And if you want to know more about this topic you can go to the link following. There’re any reply has talk about this skill.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/090bf80f-85b3-4952-99d6-9000d792529f

     

    And the solution from Rod Stephens is also a good one to give you the expect result.

     

    If there’s anything unclear please feel free to let me know, and I am glad to help you.

     

    Have a nice day!

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    Monday, August 30, 2010 1:19 PM
    Moderator
  • thanks for all your reply,the problem solved
    Friday, September 17, 2010 3:50 AM