(VB.NET 2003 app.)
I have a Login Form and when the user clicks the OK button and the password gets verified, the Main Form gets loaded. I am not able to close the Login Form when the Main Form gets Loaded. Or alternatively, when the OK button of the Login Form gets clicked and the password verification is over, the Login Form should close first and then the Main Form should be loaded.
(As it usually happens in any application).

Problem unloading Form after next Form loads.
RobertBarnes
I'll find a machine with 2003 on it and double-check; I only have 2005 currently installed. If that property is not there, the code to support it should be available, and I'll let you know. (I apologize in advance; it might be tomorrow before I get a chance to respond.)
--Matt--*
hocki101
I tried your first code, but the problem persists. Here was your code:
---------------------------------------
Dim MainFormInstance As New MainForm
Me.Hide()
MainFormInstance.Show()
Me.Close()
-----------------------------------------------------------
In line 4 where you have given Me.Close(), the Me is now MainForm, not the LoginForm from where the code was run. When I executed the code from the Login Form's OK Button, the application closed immediately.
Jeff A Beck
Hi, K.,
The problem is that one window has to be marked as the start-up object, and (by default) when it closes, the whole application shuts down -- thus, just closing the login form could lead to trouble. You have a couple of ways you can go here.
(1) You can go to the project properties and change the shutdown mode to be "When last form closes" (I believe this exists in 2003.) Next, in whatever code handles the "OK" click for the login window, add the following code:
Dim MainFormInstance As New MainForm Me.Hide()MainFormInstance.Show()
Me.Close()This is a really easy way to do what you want, without major modifications to your existing code.
(2) Or, you can make the *main* form the startup object (you can set this from the project properties), but set it to visible=false. Then, double-click the background of the main form to take you to its Load event, and in that event, Dim the login form and use ShowDialog() to show it modally. When that window returns, analyze the results to see if a correct login ultimately occurred, and if it succeeded, then show the main form by calling Me.Show() and populate it with whatever data is needed. Everything just works normally after that, and your app stays alive until the main form is closed down. (If it were me doing this, I wouldn't bother even setting the Visible property to false -- I'd just delay populating it with any data until a successful login.)
There's a third alternative, which is to control both windows from a Main() sub, but that's really messy as it requires you to do message pumping -- bleah.
Hope this helps,
--Matt--*
Ximena Cardenas
I am not able to find the option "When last form closes " in VB.NET 2003. Please confirm whether it exists or not. If yes, where it is located and if not what is the solution for VB.NET 2003.
John Penberthy
This works fine for me. (In fact I just tried it again.) Did you remember to change the project's shutdown mode to "When last form closes " If you don't, then the application will indeed shut down when Me.Close() is run.
Note that the value of "Me" can't change. "Me" will always refer to the form that owns the method you're running.
Just to summarize:
- The startup form is the Login form.
- The shutdown mode is "When last form closes."
- The "OK" method for the Login form will call the above 4 lines of code after determining that the login credentials are OK.
--Matt--*
JLucak
OK, my mistake. I'd thought this was in 2003, but it's only in 2005. I apologize for the confusion! (All the versions run together in my mind!)
In 2003, what you can do is to create a class which has a Sub Main in that. Use Sub Main as the startup object. In Main, you should create the forms that you need ("Dim myform as new LoginForm", etc.), in the order you need them, and then for the final form(in your case, your main form that you show after the login succeeds), call Application.Run(mainform) instead of mainform.show(). When mainform exits, your app exits. (To give credit where credit is due, I actually got my memory jogged on this while looking at a blog at http://blogs.vbcity.com/xtab/archive/2006/03/07/5872.aspx.)
Let me know if this helps!
--Matt--*