您不想處理啓動窗體的Load事件,因爲啓動窗體已在該階段顯示。你想把代碼放在你的Main方法中,這是創建啓動表單的地方。以下是主要方法,在Program.cs文件,貌似默認:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
您只需環繞那些三行代碼的「if」語句。 Application.Run調用會阻塞,直到它傳遞的表單關閉,此時Main方法完成並且您的應用程序退出。如果您沒有進行該Application.Run調用,那麼Main方法會立即完成,並且應用程序會關閉而無需創建,而不必介意顯示一個啓動窗體。
謝謝,這個工程。 – user2705775