Results 1 to 8 of 8

Thread: Best way to close one form and open another?

  1. #1

    Thread Starter
    Junior Member RuSty420's Avatar
    Join Date
    Dec 2007
    Posts
    21

    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

  2. #2
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    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

  3. #3

    Thread Starter
    Junior Member RuSty420's Avatar
    Join Date
    Dec 2007
    Posts
    21

    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.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    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:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.      MainForm.show
    3.      me.close  
    4. end sub

    don't forget - Project-->Properties-->Application-->Shutdown Mode - make sure its set to: when last form closes

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    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.

  6. #6
    New Member
    Join Date
    Oct 2008
    Posts
    13

    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).

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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
  •  



Click Here to Expand Forum to Full Width