-
Oct 30th, 2008, 03:52 PM
#1
Thread Starter
Junior Member
Best way to close one form and open another?
Hi,
I am writing a program that has a simple splash/login screen as its initial form. It is set as the startup form in the project settings.
Then basically what I had it do was if the username + password are correct and the user presses enter, it hides itself and shows the main application form.
Code:
Private Sub PasswordTextBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles PasswordTextBox.KeyDown
If e.KeyData = Keys.Enter Then
If UsernameTextBox.Text = "[Username]" Then
If PasswordTextBox.Text = "[Password]" Then
MainForm.Visible = True
Me.Visible = False
End If
End If
End If
End Sub
The problem with this is that it only hides itself, doesn't close itself, and when I am all finished with the main application form and I close it, the program stays open yet invisible.
So to remedy this I added this code in the MainApp.vb Form:
Code:
Private Sub MainForm_Dispose(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Disposed
Me.Close()
SplashScreen.Close()
End Sub
This seems really inefficient and unneeded to me. Is there anyway to actually close the login/splash screen, so that when the main form is closed, the program is closed?
Thanks,
-RuSty
-
Oct 30th, 2008, 04:15 PM
#2
Frenzied Member
Re: Best way to close one form and open another?
Put simply, this is a design error. The splash screen should never be set as the main form (the form that is initialized first). IMHO, any application which requires user validation should start in a Sub Main.
Please rate helpful ppl's posts. It's the best 'thank you' you can give
-
Oct 30th, 2008, 04:23 PM
#3
Thread Starter
Junior Member
Re: Best way to close one form and open another?
Ok, back to the drawing board.
So how do I go about delaying the load of the MainForm.vb form until the splash screen has completed?
Last edited by RuSty420; Oct 30th, 2008 at 04:29 PM.
-
Oct 30th, 2008, 05:45 PM
#4
Re: Best way to close one form and open another?
you can use the splashscreen as your startup form.
add a timer to your splashscreen, + set it enabled = true + set the interval accordingly. in your timer_tick event:
vb Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MainForm.show
me.close
end sub
don't forget - Project-->Properties-->Application-->Shutdown Mode - make sure its set to: when last form closes
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 30th, 2008, 05:50 PM
#5
Re: Best way to close one form and open another?
i just read your 1st post again, but the way of closing + opening the forms is the same + you still need to check your shutdown mode.
hiding forms when you don't need to is bad programming.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 30th, 2008, 05:52 PM
#6
New Member
Re: Best way to close one form and open another?
You don't have to go back to the drawing board. Simply instantiate and display the splash screen form in your Sub Main. Pass a boolean value back to Sub Main from the splash screen letting it know if the user entered a correct username/password. Sub Main then has the option to display the main form (if the user successfully logged in) or exit gracefully out of the application (if the user entered incorrect login information).
-
Oct 30th, 2008, 05:54 PM
#7
Re: Best way to close one form and open another?
you don't need to use a sub main. you can do it the way you were doing it but you should read post #4 + #5
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 30th, 2008, 07:32 PM
#8
Re: Best way to close one form and open another?
I'm afraid I can't endorse any of the advice provided so far. What you're talking about is not a splash screen, for a start. A splash screen doesn't accept input from the user.
If you want to display a true splash screen then that functionality is built into VB 2005+. You simply add your main form and your splash screen to your project, then you go to the Application tab of the project properties and select the main form as the Startup Form and the splash screen as the Splash Screen. That's it. You simply run the project. The spalsh screen will be displayed for 2 seconds, or until the main form finishes loading, at which point the splash screen form disappears and the main form appears. You don't have to write any code.
Now, if what you're actually showing is a login screen then that's different. It's still easy though, but you don't do it any of the ways advised so far. Follow the WinForms Login link in my signature for an explanation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|