Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
need help..

how do you make a form close itself in visual basic 2010 and open another form?

example... i have a login program and when the user clicks the submit button, another form will appear and the login form itself will close....



pls help...

thanks
Posted

This is one of the worst designs somehow popular in newbies. The problem is: you cannot close main form without closing of the application. Look at you entry point (usually Program.Main). Look at the line with Application.Run; the argument of this method define which form is the main one. You cannot change the main form without exit of the application. You actually can do it in one program: start another Application in the same Main method. It is very difficult to implement this logic in a flexible way.

By these reasons, the usual solution is hiding form instead of closing using Form.Hide. You can also prevent closing a form by overriding the method OnFormClosing by assigning a value of true to the property Cancel of the event arguments.

It would make the application impossible to exit, So don't forget to call Application.Exit explicitly from some menu item or elsewhere.

I recommend to change the whole approach. Try to work with just one forms but showing different parts of UI and hiding others. One of the nice styles is the tab UI based on TabControl. What was you second (third, etc.) form will go to a separate tab, that's it. It's actually also much easier to implement correctly.

—SA
 
Share this answer
 
v2
Do a Form.Close() in the submit button click event to close the form.

To open another form, you need to decide where that should be done. I would suggest that one possibility is to make the login form a modal dialog, then after validating, open the new form in the same procedure that opened the login form after ensuring that the login was valid, etc. I would probably check for a valid login in the login form's submit button event handler, then pass the result as OK, Yes, No, etc. as a DialogResult to the calling procedure when the modal dialog returns.

How you choose to do it depends on your design.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Jul-11 1:05am    
Walt, I voted 4. The problem is not so simple, because of main form.

Please see my answer.
I would recommend OP to re-design UI and go away from this approach.
--SA
Dr.Walt Fair, PE 3-Jul-11 1:10am    
I agree! I would not make the login form the main form - no way!

I assume the main form would popup the login form, which I would make a modal dialog, then handle things in the procedure that displays the login form. That way the main form could do whatever it needs to do to keep things in order and exit gracefully if the login fails, etc.

If the OP is really wanting to close the main form and popup another one, then he needs to rethink his design!
Sergey Alexandrovich Kryukov 3-Jul-11 2:09am    
We agree. To rethink the design is the best idea in this case.
--SA
Member 8033321 3-Jul-11 10:28am    
thanks everyone.... =)

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