How do I get 2 buttons on a form, the first one moves to form2, the second quits the app?

Im a newbe big time. I'm working on an RPG (to help develop my skills) that features 3 buttons on form1. The first is a button which is suppose to close form1 and open form2 for charicter creation. The second button is a credits button which simply pops a messagebox saying I made the game (which this button works), but button 3 is suppose to quit the program altogether. How do I get button1 to close form1 and open form2, and how do I get button3 to quit the program

Please be very specific (like, dumb it down!). I have only experence with QBASIC and just stared with C# like 4 days ago. Thank you for your time and patience.




Answer this question

How do I get 2 buttons on a form, the first one moves to form2, the second quits the app?

  • Bruce Nichelson

    if you close form 1, your main form, your entire app would probably close or rather still be running in the background but no forms to show. you can hide the main form or use ShowDialog() to show the other forms which means the user cant access the caller form until they closed the form on top.

    to open form2:

    Form2 theSecondForm = new Form2();

    theSecondForm.ShowDialog();

    to show a messagebox:

    MessageBox.Show("Message here", "Title", MessageBoxButtons.OK, MessageBoxIcon.Information);

    To exit the application:

    Application.Exit();

    does this help



  • tackett

    thank you


  • Charlene Reeves

    thats not the correct way of doing things.

  • Simon Dahlbacka

    hello

    How do I get button1 to close form1 and open form2

    Form2 f = new Form2();

    Form1.ActiveForm.Visible=false;

    f.Show();

    how do I get button3 to quit the program

    Form1.ActiveForm.Close();


  • Will Merydith

    Application.Exit() correctly exits the application.

    Form1.ActiveForm.Close() closes an active form but not exit the application

    the other code, Form1.ActiveForm etc.... can lead to an unknown closure of a form, meaning you maybe expecting to close a form but it closed a different form...best to make sure you have the correct reference of a form you wish to hide/close/handle with. It prevents a few problems during development also and saves headaches.

    Sorry couldnt reply in the same reply I had posted...had some ISP issues



  • Chaman Zinga

    no worries, glad I could help. I like to try to help people understand which way is correct/wrong and why that may be - we all learn and its good to share each others experience and knowledge :-)

  • Brain_Dead_Mind

    Hmmm... I found that both ways worked. Why was the first solution incorrect

  • mario.muja

    never mind! I figured it out! Thank you very much for the help!

  • BensonFabonan

    thats not the correct way of doing things

    why


  • Marc Jones

    Which first solution

    As explained, Application.Exit() is the correct method to exit your application even though stating the first method (Form1.ActiveForm.Close()) may seem like its exited the application, sometimes it may not but still in the background running away...



  • GyrusGM

    Yes, it is a great help. I forget how to hide a form (form1 in this case.) Can you give me a refresher

    Thank you!



  • How do I get 2 buttons on a form, the first one moves to form2, the second quits the app?