none
C# open a new form, and close a form...

    Question

  • Well... i want to do this: open a form and close the form that is open...
    Example:
    Form_P f = new Form_P();
    f.Show();
    this.Close();

    But, my program close.. why?
    Monday, January 22, 2007 2:41 AM

Answers

  • Hi jaomello: we have two ways for this problem:

    #1, Registering Form1.Closing event, cancel it and hide the main form

     

    #2, Start another thread for the second form:

     

    public static void ThreadProc()

    {

        Application.Run(new Form());

    }

     

    private void button1_Click(object sender, EventArgs e)

    {

        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));

        t.Start();

    }

    Beat regards!

    Monday, January 22, 2007 3:34 AM

All replies

  • Hi jaomello: we have two ways for this problem:

    #1, Registering Form1.Closing event, cancel it and hide the main form

     

    #2, Start another thread for the second form:

     

    public static void ThreadProc()

    {

        Application.Run(new Form());

    }

     

    private void button1_Click(object sender, EventArgs e)

    {

        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));

        t.Start();

    }

    Beat regards!

    Monday, January 22, 2007 3:34 AM
  • Thanks a lot, bob!
    Monday, January 22, 2007 4:21 AM
  • Thanks, but how do you close the newly opened form?

    Sorry for the silly question but I am a n00b to C#. I have only programmed properly in VB.net and C++

    Tuesday, January 23, 2007 9:36 PM
  • Hi KlmSoftware for vb quite similar as follow:

    sorry for C++ , let me ask some guys for help:)


    Imports System
    Imports System.Threading
     
    Public Class Form3
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim FM2 As New Form2
    Dim th1 As Thread
    th1 =
    New Thread(New ThreadStart(AddressOf run))
    th1.Start()
    Me.Close()
    End Sub
    Private Sub run()
    Application.Run(
    New Form2())
    End Sub
    End
    Class

    Wednesday, January 24, 2007 1:59 AM
  • If your intent is to just to hide the form, you can use Form.Hide(). Or if you can do EventArgs.Cancel = false  in the FormClosing event.
    Wednesday, January 24, 2007 2:50 AM
  • Thanks for the reply but what i ment by is that i want to know how to close a form in C#. I know how to in vb.net...

    This is what i mean:

    frmMain starts a new frmExample

    Now, how do you make it that frmMain CLOSES frmExample when a button is pressed?

    Thanks for any feedback!

    Wednesday, January 24, 2007 3:28 AM
  •  Nabeel Allana wrote:

    Thanks for the reply but what i ment by is that i want to know how to close a form in C#. I know how to in vb.net...

    This is what i mean:

    frmMain starts a new frmExample

    Now, how do you make it that frmMain CLOSES frmExample when a button is pressed?

    Thanks for any feedback!



    I have the same problem. I tried Response.Redirect("http://localhost:2110/frmForm2.aspx"); in a button click event in frmForm1, then I got a extreme long time to open the form2, almost 1 minute.
    Friday, May 02, 2008 3:31 PM
  • "this" = the current form that you are on.

     

    Form2.StartPosition = FormStartPosition.CenterParent;

    this.Hide()

    Form2.ShowDialog();

    this.Close();

     

    Best,

     

    MIS Dude

    Wednesday, December 03, 2008 6:43 PM
  • Well... i want to do this: open a form and close the form that is open...
    Example:
    Form_P f = new Form_P();
    f.Show();
    this.Close();

    But, my program close.. why?


    Form_P f = new Form_P();
    f.Show();
    f.Close();


    Monday, May 11, 2009 8:38 AM
  • Thanks Bob
    It really helped

    Kamran Shahid Senior Software Engineer/Analyst (MCP,MCAD,MCSD.NET,MCTS,MCPD.net[web])
    Friday, November 06, 2009 10:35 AM
  • Yeah!

     it really worked.

    --
    Thanks
    Gaurav K Arya
    Tuesday, December 01, 2009 2:56 PM
  • Well... i want to do this: open a form and close the form that is open...
    Example:
    Form_P f = new Form_P();
    f.Show();
    this.Close();

    But, my program close.. why?
    I'm sure you got it figured out by now but for all the people who come across this forum, his problem is "this.Close();". It should in fact, be "f.Close();". "this.Close()" will close the form you are trying to call from. "f.Close();" will make the proper call to the forms closing method.

    Willshire
    Thursday, February 04, 2010 4:28 AM
  • thnx for the reply
    Sunday, April 25, 2010 4:35 PM
  • Hi Everyone, Just have a question wonder if anyone can help me, I have a form called Login and a form called Chat and well when the user has logged into it, it should take them to the Chat form, I am using the show and hide method this works, but when your on the Chat form and you click the close button the form closes but it is still running in the processors can anyone help me to fix this problem and have it stop working all together.
    Wednesday, June 02, 2010 9:44 PM
  • Your main form must be still running ... the main form being the one called with Application.Run, e.g. Application.Run(new Form1());

    try calling Application.Exit(); when you close the chat form,that should be the simplest way. Another common  scenario would be to show the Login form again, clear password and wait for the user to either close it (thus exiting application) or login again and display Chat form.

    P.S. In future please start a new thread with a new question ... thanks.

    Best regards,
    Vladimir

    Wednesday, June 02, 2010 10:06 PM
  • you must be hiding your login form as you creates and transfer control to chat form. right where you crated the chat form object, add an new event handler to FormClosed event of chat form and in this new event, just write this.show.
    Tuesday, July 12, 2011 1:19 AM
  • Superb article

     

    Friday, October 28, 2011 4:42 AM
  • Hi Bob,

    It Helps a lot. Thanks!

    Rgds,

    Pa'1'


    Regards, Pavan N Kumar
    Monday, October 31, 2011 8:49 AM
  • I know this is an old thread but people still seem to be using it so I'll post anyways. Here is the way I do it which I feel is much better than a lot of the solutions in this thread. Here's a code snippet you would put in Program.cs that runs a login form and then the main form of the application if the login works.

    LoginForm loginForm = new LoginForm();
    Application.Run(loginForm);
    if (loginForm.DialogResult == DialogResult.OK)
        Application.Run(new MainForm());

    Monday, October 31, 2011 8:59 PM
  • "Best* Regards!"

     

    And, How do you pass all data to the second form,

    For example, I have a log in window for a MySQL server and want to stay logged in and open the next window.

    Would it all just carry over? It seems like starting a new thread would break the relationship.

    Saturday, November 26, 2011 12:10 AM
  • i wanna close login form and start welcome form. Please Describe your code ...... i hope your code may work.........
    Thursday, December 22, 2011 7:11 PM
  • Thank  Bob zhu - SJTU !

    But i want

     public static void ThreadProc(string name)

    {

        Application.Run(new Form(name));

    }

    ???

    Thank!




    • Edited by Tình Phong Wednesday, December 28, 2011 12:29 PM
    Wednesday, December 28, 2011 12:27 PM
  • A little late but it's easy to pass data between the forms. If you have a lot of data you can create a custom object to store what you need. Then you add a property of that type to the login form, and pass the information into the constructor of your next form.

    LoginForm loginForm = new LoginForm();
    Application.Run(loginForm);
    
    MyCustomDataClass loginData = loginForm.LoginData;
    
    // One way to check
    if (loginForm.DialogResult == DialogResult.OK)
        Application.Run(new MainForm(loginData));
    
    // A different way to check
    if (loginData.Success)
        Application.Run(new MainForm(loginData));
    

    Monday, January 02, 2012 2:11 PM
  • Thankyou very much!
    Friday, January 06, 2012 12:48 PM
  • Thank you... It works...
    Tuesday, January 31, 2012 11:29 AM
  •    

    This code works good and is easy,

    when i click on close button, present form will hide and new form that is Login form will get open according to this code.

         private void btnClose_Click(object sender, EventArgs e)
            {

                FrmLogin f = new FrmLogin();
                this.Hide();
                f.Show();

            }                   

                            
    Wednesday, June 20, 2012 7:46 AM
  • Excellent Method BOB
    Sunday, August 12, 2012 5:58 PM
  • well you do not need to write this.Close().if you will write this.Close() then your both forms will get close.so instead of Close() you have to write Hide().

    for example.

    Form_P f=new Form_P();

    f.show();

    this.Hide();

    it will work .

    all the best

    Sunday, September 30, 2012 4:48 PM
  • t.whatever_method_it_is_to_stop_the_thread.
    Sunday, December 23, 2012 8:50 PM
  • public class frmMain : Form
    {
    	public frmMain()
    	{
    		InitalizeComponent();
    	}
    
    	private void button1_Click(object sender, EventArgs e)
    	{
    		frmExample.ActiveForm.Close();
    	}
    }


    • Edited by CoolKile08 Sunday, December 23, 2012 8:58 PM
    Sunday, December 23, 2012 8:55 PM
  • Well... i want to do this: open a form and close the form that is open...
    Example:
    Form_P f = new Form_P();
    f.Show();
    this.Close();

    But, my program close.. why?

    I'm sure you got it figured out by now but for all the people who come across this forum, his problem is "this.Close();". It should in fact, be "f.Close();". "this.Close()" will close the form you are trying to call from. "f.Close();" will make the proper call to the forms closing method.

    Willshire

    Form_P f = new Form_P();

    f.Show();

    f.Close(); will just open the other class (f), then close it again. Well actually, Form.Show() doesn't even SHOW the form for some reason. The working way is:

    Form_P f = new Form_P();

    f.ShowDialog();

    this.Close();

    Sunday, December 23, 2012 10:22 PM
  • If you want to keep it easy you could just do it in the Program.cs (default location of the main-function in VS).
    Untouched it looks like this:

            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }

    If you want to open another form when the "new Form1()"-object closes then you could edit it to this:

    static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                Application.Run(new MySecondForm());
            }
    Have fun ;)
    Sunday, December 23, 2012 11:34 PM
  • it is not in that

    complicate

    The simplest way

    frm f2 = newfrm();

    this.Hide();

    f2.ShowDialog();

    this.Close();

    Friday, June 21, 2013 7:49 PM
  • Well first of all you must define at public class:
    public Form1()
            {
                InitializeComponent();
            }
            Form2 frm2 = new Form2();Now here you have all code:
     public Form1()
            {
                InitializeComponent();
            }
            Form2 frm2 = new Form2();

            private void Form1_Load(object sender, EventArgs e)
            {
                IsMdiContainer = true;
         
                frm2.Show();
                frm2.MdiParent = this;
                

            }

            private void newToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
            }

            private void closeToolStripMenuItem_Click(object sender, EventArgs e)
            {
                frm2.Close();
            }
        }
    }
    Thursday, June 27, 2013 6:56 AM
  • Whats the meaning of the code starting from button1 onwards. whats threading?
    Sunday, July 21, 2013 3:58 AM
  • men, you save my life.

    How I give you points???

    Monday, August 12, 2013 2:10 AM
  • Thanks.. Guys.

    U r right.. easy way to close and open.

    this.Hide();

    // To call main page.
                    Zeal_Form_Main_Menu Lr_Zeal_Form_Main_Menu = new Zeal_Form_Main_Menu();
                    Lr_Zeal_Form_Main_Menu.Show(); 

    Note : Close this windows by this code. because still ur login page is hidden.

     Application.Exit();

    Monday, October 21, 2013 5:53 PM
  • you also could hide your form first. then put a new form on screen. and close your first screen then.

    this will make your program faster when it has a lot of forms. the code will look like this 

    form2 ff = new form2();

    this.Hide();

    ff.Show();

    form1 f = new form1();

    f.close

    Wednesday, November 20, 2013 4:30 PM
  • srry i mean:

    f.Close();

    Wednesday, November 20, 2013 4:31 PM