我想在打开主窗体后打开第二个窗体,但第二个窗体首先打开

时间:2013-11-11 00:13:49

标签: c# forms

我正在尝试在主窗体显示后立即打开辅助窗体,但是,辅助窗体首先显示,然后在辅助窗体关闭后显示主窗体(第二种窗体就像启动画面一样)。这是一个例子:

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...
    }

3 个答案:

答案 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);
}

或者,取决于doSomething1doSomething2是否正在进行用户不需要查看的后台处理:您可以在加载处理程序中保留这两个,并且仅移动最后两个语句进入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();