Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
How do I close a windows app form and open another windows app form and repeat it?

When I write for example
form1 frm= new form1();
frm.close();
form2 frm2=new form2();
frm2.showdialog();



form1 isn't close but form2 opened.
Posted
Updated 9-Jan-11 6:01am
v3
Comments
Dalek Dave 9-Jan-11 12:01pm    
Edited for Readability.
Sergey Alexandrovich Kryukov 9-Jan-11 13:38pm    
Bad idea. Why would you do it?

Because you declare a new instance of form1 and immediately close it:
form1 frm= new form1();
frm.close();
Nothing happens - it doesn't close because you never opened it.
If you are running in form1 when you close the current instance (which is what you appear to be trying to do) then your app will shut down.

Try this instead:
this.Hide();
Form2 frm2 = new Form2():
Form2.ShowDialog();
this.Show();
It will make it look as if your form disappears and the new form appears.
You don't actually need the this part of Hide or Show - I just put them in to show you what they were acting on.
 
Share this answer
 
Comments
ely z 9-Jan-11 8:29am    
it's good
thank's alot
ely z 9-Jan-11 8:45am    
this.hide is not true
because i have a timer in my project therefor when write hide and show several form are opened.
Dalek Dave 9-Jan-11 12:01pm    
Good Answer.
Most windows forms applications includes a class Program, often defined in Program.cs similar to

C#
static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }


Main is the entry point of you application, and Application.Run(new MainForm()); creates and displays an instance of MainForm.

I expect you can take it from here :)

Update:
Application.Run is also responsible for executing the Windows message loop ...

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Dalek Dave 9-Jan-11 12:02pm    
Good Call.
Espen Harlinn 9-Jan-11 16:40pm    
Thanks Dalek!
Are you talking about MDI application?If so,You have to set the IsMdiContainer property of form1 to make it as the container for multiple child forms

C#
this.IsMDIContainer = true;


//To Create MDI Child Forms,say your form2

Form frmchild=new Form();
frmchild.MDIParent=this;
frmchild.Show();

Check this :
Creating MDI application using C# (Walkthrough)[^]
 
Share this answer
 
Comments
Dalek Dave 9-Jan-11 12:02pm    
Good Call.
Anupama Roy 10-Jan-11 13:05pm    
Thanks Dalek!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900