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

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

  • Monday, January 22, 2007 2:41 AM
     
     
    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?

All Replies

  • Monday, January 22, 2007 3:34 AM
     
     Answered

    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 4:21 AM
     
     
    Thanks a lot, bob!
  • Tuesday, January 23, 2007 9:36 PM
     
     

    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++

  • Wednesday, January 24, 2007 1:59 AM
     
     

    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 2:50 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 3:28 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!

  • Friday, May 02, 2008 3:31 PM
     
     
     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.
  • Wednesday, December 03, 2008 6:43 PM
     
     

    "this" = the current form that you are on.

     

    Form2.StartPosition = FormStartPosition.CenterParent;

    this.Hide()

    Form2.ShowDialog();

    this.Close();

     

    Best,

     

    MIS Dude

  • Monday, May 11, 2009 8:38 AM
     
     
    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();


  • Friday, November 06, 2009 10:35 AM
     
     
    Thanks Bob
    It really helped

    Kamran Shahid Senior Software Engineer/Analyst (MCP,MCAD,MCSD.NET,MCTS,MCPD.net[web])
  • Tuesday, December 01, 2009 2:56 PM
     
     
    Yeah!

     it really worked.

    --
    Thanks
    Gaurav K Arya
  • Thursday, February 04, 2010 4:28 AM
     
     
    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
  • Sunday, April 25, 2010 4:35 PM
     
     
    thnx for the reply
  • Wednesday, June 02, 2010 9:44 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 10:06 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

  • Tuesday, July 12, 2011 1:19 AM
     
     
    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.
  • Friday, October 28, 2011 4:42 AM
     
     

    Superb article

     

  • Monday, October 31, 2011 8:49 AM
     
     

    Hi Bob,

    It Helps a lot. Thanks!

    Rgds,

    Pa'1'


    Regards, Pavan N Kumar
  • Monday, October 31, 2011 8:59 PM
     
     

    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());

  • Saturday, November 26, 2011 12:10 AM
     
     

    "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.

  • Thursday, December 22, 2011 7:11 PM
     
     
    i wanna close login form and start welcome form. Please Describe your code ...... i hope your code may work.........
  • Wednesday, December 28, 2011 12:27 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
    •  
  • Monday, January 02, 2012 2:11 PM
     
      Has Code

    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));
    

  • Friday, January 06, 2012 12:48 PM
     
     
    Thankyou very much!
  • Tuesday, January 31, 2012 11:29 AM
     
     
    Thank you... It works...
  • Wednesday, June 20, 2012 7:46 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();

            }                   

                            
  • Sunday, August 12, 2012 5:58 PM
     
     
    Excellent Method BOB
  • Sunday, September 30, 2012 4:48 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, December 23, 2012 8:50 PM
     
     
    t.whatever_method_it_is_to_stop_the_thread.
  • Sunday, December 23, 2012 8:55 PM
     
      Has Code
    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 10:22 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 11:34 PM
     
      Has Code

    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 ;)