Closing forms in VB.NET
I have a small requirement and that is as follows:
I have a login form and after a successful login, i open another MDI form, but i want to close the login form. I have given the code as:
MDIForm1.show
Me.Close
This does not seem to work.
Can anyone help on this.
Similarly, is is possible to open the login form on an inactive MDI form [MDI and Login for to be seen] and when th user login details are correct, it must close the login form and return开发者_StackOverflow社区 to the MDI Form.
Please help on this.
Regards,
GeorgeFirstly, I'm guessing you've set your login form as the application's startup form? If so then VB has put something like this together for you behind the scenes:
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New LoginForm)
The magic happens there in that Application.Run
call, which basically creates a message loop for your GUI and terminates when it detects that your form has been closed.
There are a couple of ways that I know of to deal with this.
- First: don't make the login form your startup form at all. Create your main form, but cause it to hide itself and
ShowDialog
on an instance of your login form before appearing. - Make your login form your startup object, but instead of closing it when you open your main form, simply hide it (this is actually pretty horrendous to me personally, but it's a possibility).
As for the MDI form question: this seems completely possible to me, if I understand you correctly. You want to show both the login form and the MDI form at once, but you want the MDI form to be disabled until the login form is closed, right?
So just make the MDI form your application's startup form, have it call ShowDialog
on your login form in its Load
event, and bingo: the MDI form will effectively be "locked" until the login form is dealt with (this is how modal dialogs in general such as MessageBox
work). This is actually the same as option #1 above except that the main form (your MDI form) is never hidden, only blocked.
Other ways:
Dim <form name> As New <form name>
<form name>.Show()
Me.Hide()
精彩评论