-3

I want to open one form through another form by means of a button and close current form. i found how to do that but i tried my way and could not do that. why this code does not work

private void Button1_Click(object sender, EventArgs e)
    {

            Advise1 form = new Advise1();
            form.Show();
            this.Close();


    }

I know how to solve my problem but i want to know why close() method do not close() first form. when i try this way two form are open after click on button.

1 Answers1

1

You don't say what your problem or error was, but the answer is likely in your main program's entry point - usually in program.cs:

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 form1 = new Form1();
        Application.Run(form1);
    }
}

Application.Run(form1); is basically saying "this instance of Form1 is my application, keep running until this form closes."

When you invoke this.Close(); you are closing your Form1 instance which is basically closing your whole application down.

AlwaysLearning
  • 7,915
  • 5
  • 27
  • 35
  • I tried this and receive error "'SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application.'" –  Aug 08 '19 at 09:41
  • I know how to solve my problem but i want to know why close() method do not close() first form. –  Aug 08 '19 at 09:45
  • close() method in this way does not close whole app. –  Aug 08 '19 at 09:45