如果我想做某事,我通常使用以下模式。表单完全加载后:
public partial class BaseForm : Form
{
public event EventHandler Loaded;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Application.Idle += OnLoaded;
}
protected void OnLoaded(object sender, EventArgs e)
{
Application.Idle -= OnLoaded;
if (Loaded != null)
{
Loaded(sender, e);
}
}
}
如果我从 BaseForm 派生我的主要形式,我有一个Loaded
事件,在你的情况下,我将使用如下。
static class Program
{
[STAThread]
static void Main()
{
var mainForm = new MainForm();
mainForm.Loaded += (sender, e) => { new LoginDialog().ShowDialog(mainForm); };
Application.Run(mainForm);
}
}