Simple form problem

W

Water Cooler v2

I have a frmLogin form and a frmMDI form.

In the Login button's click event in the Login form, I write:

frmMDI frm = new frmMDI();
frm.Show();
this.Close();

This causes the frmMDI to show up and then the application to exit. I
am suspecting this is because:

1. The frmLogin is the startup object that has the entry point Main();
and
2. The frmMDI class's object frm is a local reference inside the
btnLogin_Click event handler inside the frmLogin form. Hence, as soon
as the frmLogin closes, the reference to frmMDI gets garbage collected.

Is that suspicion true? If yes, then the way to get around this would
be to have a global reference to frmMDI somewhere in some global
namespace and just to allocate memory to that frmMDI within frmLogin
and just to show it in there and exit frmLogin.

Is that the way to go here? If yes, where do I declare the reference to
the frmMDI form?

PS: I do have a Globals class with all static members that are global
to the entire application. Should I put the reference to frmMDI in
there?
 
M

Marc Gravell

Active forms will not be garbage-collected; they are counted as
visible.

The problem here is that your main thread is exiting; when your last
(non-background) thread exits, the app ends. Period.

Try replacing this with:
this.Close();
Application.Run(frm);

i.e. end the first form, then run the second form until it ends; you
could also use frm.ShowDialog() here (instead of Application.Run).

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top