I created 2 winforms in one i create a button .when i click the button i want to go to the next form(form2) and close the form1.

plz give me the code .

this is my code it is not working

form1

namespace MyTest1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void formcls()
        {
            this.Close();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 fm2 = new Form2();
            fm2.Show();
            
        }
    }
}

form2

namespace MyTest1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            Form1 fm = new Form1();
            fm.formcls();

        }
    }
}

Your design sounds a lot like a login dialog or splash screen. The problem with closing a form is that if it is the main form for the application that was passed to Application.Run(), closing it will terminate the application. Another problem for login boxes is loading the dialog from the main form's Load event will still flash the main form's window even if the login fails and the application exits.

As long as the initial form is not needed after the main form is loaded, something like the following works a treat. :)

using System;
using System.Windows.Forms;

namespace Ed {
    static class Program {
        [STAThread]
        static void Main() {
            RunApplication();
        }

        static void RunApplication() {
            AppDomain.CurrentDomain.UnhandledException +=
                delegate(object sender, UnhandledExceptionEventArgs e) {
                    MessageBox.Show("Load Error: " + e.ExceptionObject.ToString());
                    MessageBox.Show("An error was detected at load time", "Load Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);

                    if (Application.MessageLoop)
                        Application.Exit();
                    else
                        Environment.Exit(-1);
                };

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (new Login().ShowDialog() == DialogResult.OK)
                Application.Run(new Main());
        }
    }

    class Main : Form { }

    class Login : Form {
        private Button buttonOk, buttonCancel;
        private FlowLayoutPanel layout;

        public Login() {
            buttonOk = new Button();
            buttonOk.Text = "OK";
            buttonOk.Click += new EventHandler(buttonOk_Click);

            buttonCancel = new Button();
            buttonCancel.Text = "Cancel";
            buttonCancel.Click += new EventHandler(buttonCancel_Click);

            layout = new FlowLayoutPanel();
            layout.Dock = DockStyle.Fill;
            layout.Controls.Add(buttonOk);
            layout.Controls.Add(buttonCancel);

            Controls.Add(layout);
        }

        void buttonOk_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.OK;
        }

        void buttonCancel_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
        }
    }
}

Your design sounds a lot like a login dialog or splash screen. The problem with closing a form is that if it is the main form for the application that was passed to Application.Run(), closing it will terminate the application. Another problem for login boxes is loading the dialog from the main form's Load event will still flash the main form's window even if the login fails and the application exits.

As long as the initial form is not needed after the main form is loaded, something like the following works a treat. :)

using System;
using System.Windows.Forms;

namespace Ed {
    static class Program {
        [STAThread]
        static void Main() {
            RunApplication();
        }

        static void RunApplication() {
            AppDomain.CurrentDomain.UnhandledException +=
                delegate(object sender, UnhandledExceptionEventArgs e) {
                    MessageBox.Show("Load Error: " + e.ExceptionObject.ToString());
                    MessageBox.Show("An error was detected at load time", "Load Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);

                    if (Application.MessageLoop)
                        Application.Exit();
                    else
                        Environment.Exit(-1);
                };

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (new Login().ShowDialog() == DialogResult.OK)
                Application.Run(new Main());
        }
    }

    class Main : Form { }

    class Login : Form {
        private Button buttonOk, buttonCancel;
        private FlowLayoutPanel layout;

        public Login() {
            buttonOk = new Button();
            buttonOk.Text = "OK";
            buttonOk.Click += new EventHandler(buttonOk_Click);

            buttonCancel = new Button();
            buttonCancel.Text = "Cancel";
            buttonCancel.Click += new EventHandler(buttonCancel_Click);

            layout = new FlowLayoutPanel();
            layout.Dock = DockStyle.Fill;
            layout.Controls.Add(buttonOk);
            layout.Controls.Add(buttonCancel);

            Controls.Add(layout);
        }

        void buttonOk_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.OK;
        }

        void buttonCancel_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
        }
    }
}

Thanks a lot Radical Edward .i got the logic, but the problem is the most of keywords you used is new to me.i dont know how to handle them.i m new to c# language, Thanks a lot again Radical Edward.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.