i want to go to form2 on buttonclick of form1 and close form1 at the same time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • subrat kumarnayak
    New Member
    • Jul 2010
    • 10

    #1

    i want to go to form2 on buttonclick of form1 and close form1 at the same time

    i want to move to form2 on buttonclick at form1 and at the same time i want to close form1
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    The simplest way to accomplis this is to add this to a button event handler in form1:
    Code:
    frmTest2 myForm = new frmTest2();
    myForm.Show();
    this.Close();
    but this only works if form1 is not the main application form. If form1 is the main application form then this merely kills the application.

    To do this with all forms and have it work with the main application form you would have to add a property to your forms, say NextFormToShow, that returns a form object. Then whatever code is checking for form1 to be closed would have to show the next form.

    In Program.cs, that would mean having something like the following lines:

    Code:
                frmTest mainForm = new frmTest();
                Application.Run(mainForm);
    
                //executes after mainForm has closed
                Form nextForm = mainForm.NextFormToShow;
                Application.Run(nextForm);

    Comment

    Working...