2013-05-19 76 views
0

我製作了一個淡入淡出的表單並加載另一個表單。 當新窗體加載它時關閉啓動窗體。關閉啓動表單殺死應用程序

問題是什麼時候它關閉了這個啓動形式它殺死了我的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 

回答

4

您應該檢查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,它應該可以解決您的問題。

0

默認情況下,如果「啓動窗體」花費的時間比「加載」的時間長,則啓動屏幕保持打開兩秒或更長時間。

因此,爲了讓您的啓動畫面保持打開狀態,無論您需要多長時間,只需從加載到發出信號的主窗體。一種方法是使用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(),因爲不涉及猜測。