Closing startup form kills application

☆樱花仙子☆ 提交于 2019-12-25 05:28:12

问题


I make a form that fades in then fades out and loads another form up. when the new form has loaded it closes the the start up form.

Problem is when it closes this start up form it kills my apllication.

Public Class Splash
Dim appearance As Boolean = False

Private Sub Splash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Opacity = 0
    FadeIn.Start()
End Sub

Private Sub FadeIn_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FadeIn.Tick
    If Not appearance Then
        Opacity += 0.015
    End If
    If Opacity = 1 Then
        appearance = True
    End If
    If appearance = True Then
        Opacity -= 0.015
        If Opacity = 0 Then
            Form1.Show()
        End If
    End If
End Sub
End Class

Public Class Form1

Private Function WebLoad()
    While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
        Application.DoEvents()
    End While
    Return 0
End Function
Private Function Login(ByVal User As String, ByVal Pass As String)
    WebBrowser1.Navigate("http://caan/SC5/SC_Login/aspx/login_launch.aspx?SOURCE=ESOLBRANCHLIVE")
    WebLoad()
    WebBrowser1.Document.GetElementById("txtUserName").SetAttribute("value", User)
    WebBrowser1.Document.GetElementById("txtPassword").SetAttribute("value", Pass)

    Dim allImgTags As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
    If allImgTags IsNot Nothing Then
        For Each img As HtmlElement In allImgTags
            If img.GetAttribute("src").Contains("/images/SC5Login07.jpg") Then
                img.InvokeMember("Click")
                Exit For
            End If
        Next img
    End If
    Return 0
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Login(user, pass)
    WebBrowser2.Navigate("http://caan/SC5/SC_PartsCentre/aspx/partscentre_frameset.aspx")
    WebBrowser1.Navigate("http://caan/SC5/SC_RepairJob/aspx/RepairJob_frameset.aspx")
    Splash.Close()
End Sub
End Class

I am aware that there is a propper splash screen form but it doesnt stay open long enough. On its fadeout it just closes and launches my app

My main question is how can i stop splash.close() from closing my whole application?

Answer

Sorted this now, Wont let me answer at the bottem...

Used to splashscreen provided in visualbasic express and added a couple lines i found on msdn to the Application Events

ApplicationEvents

Namespace My

' The following events are available for MyApplication:
' 
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication
    Protected Overrides Function OnInitialize( _
ByVal commandLineArgs As  _
System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean 
        Me.MinimumSplashScreenDisplayTime = 7000
        Return MyBase.OnInitialize(commandLineArgs)
    End Function

End Class
End Namespace

SplashScreen1.VB

Public NotInheritable Class SplashScreen1
Dim appearance As Boolean = False

Private Sub SplashScreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If My.Application.Info.Title <> "" Then
        ApplicationTitle.Text = My.Application.Info.Title
    Else
        ApplicationTitle.Text = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
    End If

    Version.Text = System.String.Format(Version.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor)

    Copyright.Text = My.Application.Info.Copyright

    Opacity = 0
    Fade.Start()
End Sub

Private Sub MainLayoutPanel_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MainLayoutPanel.Paint
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fade.Tick
    Fade.Interval = 100
    If Not appearance Then
        Opacity += 0.1
    End If

    If appearance = True Then        
        Opacity -= 0.1
        End If
    If Opacity = 1 Then
        Fade.Interval = 5000
        appearance = True
    End If
End Sub
End Class

回答1:


You should check the Shutdown mode option in the application page of your project http://msdn.microsoft.com/en-us/library/tzdks800(v=vs.90).aspx and tell us the value of Shutdown mode and Startup form.

I guess Shutdown mode is set to When startup form closes and Startup form is set to Splash.

In this case, try to set Shutdown mode to On last window close and it should solve your issue.




回答2:


By default, the Splash screen stays open for two seconds, or longer if the "Startup Form" takes longer than that to "load".

Therefore, to make your splash screen stay open for however long you need, simply block the main form from loading until it is signaled. One way to do this is with a ManualResetEvent.

In this setup, the "Startup form" is Form1, the "Splash screen" is Splash, and "Shutdown mode" is When startup form closes.

In Splash, I've added a Shared ManualResetEvent and only signal it when the form is done animating:

Public Class Splash

    Private appearance As Boolean = False

    Public Shared MRE As New System.Threading.ManualResetEvent(False)

    Private Sub Splash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Opacity = 0
        FadeIn.Start()
    End Sub

    Private Sub FadeIn_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FadeIn.Tick
        If Not appearance Then
            Opacity += 0.015
        Else
            Opacity -= 0.015
        End If

        If Opacity = 1 Then
            appearance = True
        ElseIf Opacity = 0 Then
            MRE.Set()
        End If
    End Sub

End Class

Now, over in Form1, we call WaitOne() against the Shared ManualResetEvent:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim MySplash As Splash = DirectCast(My.Application.SplashScreen, Splash)
        Splash.MRE.WaitOne() ' wait for the splash to signal

        ' ... other code ...

    End Sub

End Class

Now Form1 one will not appear until the splash screen is done animating, and the application will properly have Form1 as the "Startup form" so it won't shutdown when splash closes and Form1 opens. Also note that we are not explicitly opening or closing any forms in the code; that is done automatically for us by the framework itself.

With this approach the main form won't open until the splash screen is done. Furthermore, if you change the animation length, you won't have to go in and change MinimumSplashScreenDisplayTime() as there is no guesswork involved.



来源:https://stackoverflow.com/questions/16634006/closing-startup-form-kills-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!