none
log in open form2 close form1 use form 2 RRS feed

  • Question

  • wel its like this (oh yeah iam dutch so my english is not verry good so may some times my spelling or something els is not so good) i am just having fun with programming so i thought i am gonna make a log in thingy! so i made a new poject, windows form application, and placed some buttons and some textboxes and i did some coding and it works. wel not so good cuz when i log in then the panel with the log in stuff disappears and (well i just made a simple webbrowser) my webbrowser shows up in the same form but i want that it opens in an other form but i did not know how to do that so i did it like this. but i still want to know to open an other form so i browsed on this forum and found some stuff:

     

    Form2 myStuff = new Form2

    ();

    myStuff.Show();

     

    Application.OpenForms["Form1"].Close();

    but what happens if i log in that it shuts down every thing...

    i'll show the whole coding:

    private

     

     

    void button1_Click(object sender, EventArgs

    e)

    {

     

     

    if (passwordBox1.Text.Equals("12345") && (usernameBox1.Text.Equals("lacrimis"

    )))

    {

     

     

    MessageBox.Show("your are succesfully logged in"

    );

    passwordBox1.Focus();

    usernameBox1.Focus();

     

     

    Form2 myStuff = new Form2

    ();

    myStuff.Show();

     

     

    Application.OpenForms["Form1"

    ].Close();

    }

     

     

     

     

     

     

     

    but it shuts down at the momend i clicked Ok in the messageBox

    so why does that happends and what to do about it?

    plz help me out cuz i know its basic stuff but iam not so very long a well not really a developper cuz my skills ar not good anougth to call my self a developper

    so every help is welcome.

    Sunday, August 28, 2011 3:29 PM

All replies

  • Which form is button1 on?  It looks like once you get logged in, your message box shows, you click OK, and then it falls through to this line of code:

    Application.OpenForms["Form1"].Close();
    


    Which causes Form1 to close. Apparently your main startup form is Form1, so when you close Form1, everything naturally closes down.  The reason is that Form1 is running in your main application thread, and when it closes down, then your program exits out of the Main entry point to your program.

    Instead of doing Application.OpenForms["Form1"].Close(), do Application.OpenForms["Form1"].Hide().

     


    Tom Overton



    • Edited by Tom_Overton Wednesday, August 31, 2011 1:55 AM formatting
    • Proposed as answer by Kris444 Sunday, September 4, 2011 3:48 AM
    Wednesday, August 31, 2011 1:48 AM