关闭启动窗体应用杀死(Closing startup form kills application)

2019-10-29 02:56发布

我作出这样的淡入然后淡出并加载另一种形式的一个形式。 当新的形式加载它关闭的启动形式。

问题是,当它关闭这个启动形式,它杀死了我的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

我知道,有一个propper闪屏的形式,但它没有留下足够长的时间开放。 在它的淡出,它只是关闭并启动我的应用程序

我的主要问题是,我怎么能阻止关闭我的整个应用程序splash.close()?

回答

现在这个排序,不会让我回答的bottem ...

用于闪屏在VisualBasic中明确规定,并增加了几行我在MSDN上找到的应用程序事件

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

Answer 1:

您应该检查Shutdown mode option在项目的申请页面http://msdn.microsoft.com/en-us/library/tzdks800(v=vs.90).aspx并告诉我们的价值Shutdown modeStartup form

我想Shutdown mode设置为When startup form closesStartup form设置为Splash

在这种情况下,尝试设置Shutdown mode ,以On last window close ,它应该解决您的问题。



Answer 2:

默认情况下,闪屏保持打开两秒钟或更长的时间,如果“启动窗体”需要长于“装入”。

因此,为了使您的闪屏保持打开但长期需要,简单地阻止加载的主要形式,直到它发出信号。 这样做的一个方法是使用一个ManualResetEvent的。

在这种设置中,将“启动形式”是Form1 ,在“闪屏”是Splash ,而“关断模式”是When startup form closes

在闪,我添加了一个共享的ManualResetEvent,仅表示它在形式完成动画:

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

现在,在Form1中,我们调用了WaitOne()对共享的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

现在,不会出现Form1的一个,直到启动画面做动画应用程序将正常有Form1中的“启动形式”,所以它不会关机时飞溅关闭和打开Form1中。 还要注意的是,我们没有明确地打开或关闭代码中的任何形式的; 由框架本身会自动为我们做了。

这种方法的主要形式将不会打开,直到闪屏完成。 此外,如果你改变了动画的长度,你会不会去和改变MinimumSplashScreenDisplayTime()因为没有涉及猜测。



文章来源: Closing startup form kills application