我正在尝试在主窗体显示后立即打开辅助窗体,但是,辅助窗体首先显示,然后在辅助窗体关闭后显示主窗体(第二种窗体就像启动画面一样)。这是一个例子:
private void Form1_Load(object sender, EventArgs e)
{
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
private void doSomething1(object sender, EventArgs e)
{
// Do something here...
}
private void doSomething2(object sender, EventArgs e)
{
// Do something here...
}
private void doSomething3(object sender, EventArgs e)
{
// Do something here...
}
答案 0 :(得分:1)
使用Form1.Shown
事件代替Load
事件:
private void Form1_Shown(Object sender, EventArgs e) {
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
或者,取决于doSomething1
和doSomething2
是否正在进行用户不需要查看的后台处理:您可以在加载处理程序中保留这两个,并且仅移动最后两个语句进入Shown
处理程序。
答案 1 :(得分:1)
在构造函数中首先初始化Shown
事件处理程序:
public Form1()
{
InitializeComponent();
this.Shown += new EventHandler(Form1_Shown);
}
然后把代码放在那里:
void Form1_Shown(object sender, EventArgs e)
{
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
答案 2 :(得分:0)
public Form1()
{
InitializeComponent();
Form2 f2 = new Form2();
f2.Show();