Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello all,

I recently came acorss something that I have never really given much thought to before, when opening a new form and closing the current, I usually just hide the current and Show the new form using the ShowDialog method:

Form2 nextForm = new Form2();<br />
this.Hide();<br />
nextForm.ShowDialog();<br />
this.Close();



However I was using the ShowDialog to actually get a DialogResult the other day and thought, why don't I just use the normal show method when opening and closing forms, but if I run the same code replacing ShowDialog with Show, the application closes as soon as form2 is opened.

Using the showDialog method above I am guessing that it will have the effect, that if you have a form leading onto another and another etc, all the previous forms will still be in memory and have some kind of performance impact.

With regards to the problem with using the standard Show method, I realise the reason it closes the application is to do with the close event of Form1, if it's fired the whole application will close, I have seen some people create a new process for the next form, but I don't 100% understand it.

If anyone could explain this I would be grateful, or if anyone could provide an alternate solution that'd be great.

Regards Senior Crispy.
Posted
Updated 17-Mar-23 19:05pm

You can create your own class derived from ApplicationContext. In there you can have as many forms as you like, add and remove forms etc. When forms close reduce it's form count - when 0, call ExitThread();. In program.cs, instaead of calling Application.Run(new YourForm()); pass an instnace of your custom context.
 
Share this answer
 
SeniorCrispy wrote:
Form2 nextForm = new Form2();
this.Hide();
nextForm.ShowDialog();
this.Close();


the code must be written like this though it dosen't make any difference but i always write the code for form hiding in this manner anyways
Form2 obj = new Form2();
            obj.Show();
            this.Hide();


Show dialog will set form2 as a modal dialog box

For more data u can refer this site
ShowDialog()

this.Close() when this method is called any managed resource can be easily closed, but when dispose() method is called it will permanently remove the form from the memory

Do rate my answer once you find it useful

Thanks & Regards
Radix :)
 
Share this answer
 
v2
It doesn't appear that you understand what the real difference between the Show and ShowDialog methods or how the program actually runs.

When your C# program starts, the first thing the application does is to create a new form of whichever form is your main form and then run it. The application will quit when that form is closed. From MSDN: "This method adds an event handler to the mainForm parameter for the Closed event. The event handler calls ExitThread to clean up the application."

This means that whenever your main form is closed, the application will close unless your Program.cs has anything after that call.

As to the difference between Show and ShowDialog, ShowDialog does the same basic thing. When that line of code runs, it will wait until that form has closed to run the next line of code. So with your code,
C#
Form2 nextForm = new Form2();
this.Hide();
nextForm.ShowDialog();
this.Close(); 

this.Close(); will not be hit until nextForm is closed.


SeniorCrispy wrote:
all the previous forms will still be in memory and have some kind of performance impact.


That's true. If you don't close a form, it will still be in memory. However, you have to keep your main form open or the application will close. But, honestly, keeping the main form open is really a pretty insignificant use of memory.
 
Share this answer
 
Comments
Walby 15-May-10 20:34pm    
Cheers for the reply, I think I understood the majority of that originally but didn't word my question too well sorry. Perhaps I am being to cautious and making life harder for myself then I really need to in regards to the performance impact the main form will have on the application.
If your intent is to close the first form, try this:

Application.Run(new MyNextForm());
 
Share this answer
 
SeniorCrispy wrote:
If anyone could explain this I would be grateful


i think this problem has been explained rightly.
then

SeniorCrispy wrote:
if anyone could provide an alternate solution that'd be great.


Answer3 said :
Application.Run(new MyNextForm());


i donnot think so .i support he is asking the situation like this : you hit a button on form1, then form2 pop-up and form1 dispear. the from1 can dispear in an easy way -- form1.hide().but if we need to close form1 and later operation are all going on form2,how to solve this problem? if we use form1.close(),then this application will exit from memory.but form2 will dispear too......

now the problem is -- actually close form1(dispear form memory) and open the new form -- form2 ? demo code are more appreciated :-)

p.s my english is poor , i hope you can understand it :-o
 
Share this answer
 
v2
new Form2().Show();
this.Hide();
 
Share this answer
 
C#
private void OnButton1Click(object sender, EventArgs e)
{
    this.Hide();
    var form2 = new Form2();
    form2.Closed += (s, args) => this.Close(); 
    form2.Show();
}
 
Share this answer
 
Comments
Member 12555403 2-Jul-23 13:59pm    
it works very well thank you

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