【问题标题】:Forms are not Closing [duplicate]表格未关闭[重复]
【发布时间】:2013-02-01 03:20:43
【问题描述】:

我的程序中有这些代码。但是 this.close 不起作用。并且因为前一个表格没有关闭,它与第二个表格重叠。我应该怎么做才能一次只打开一个表格?我设置了一个值来获取位于 form1 中的 lblScore 的内容以显示在 form2 上。但是,我希望关闭 form1,以便唯一打开的表单是 form2。我怎样才能做到这一点?

if (score >= 2) {
    timerDrop.Enabled = false;
    MessageBox.Show("Time's Up! You can now proceed to the next Level!");
    frmLevel2 lvl2 = new frmLevel2();
    lvl2.Show();
    lvl2.set = lblScore.Text;
    this.Close();
} else {
    timerDrop.Enabled = false;
    MessageBox.Show("Time's Up! GAME OVER!");
    frmMenu frmBackToMenu = new frmMenu();
    frmBackToMenu.Show();
    this.Close();
}

谢谢。

【问题讨论】:

  • 你可以只使用 ShowDialog() 而不是 Show()
  • @CSharpStudent ShowDialog() 是一种阻塞方法。我怀疑这就是 OP 所追求的。
  • @TiesonT。好吧,它只是在显示没有显示对话框的第二个表单后关闭第一个表单时会关闭两个表单

标签: c# .net winforms


【解决方案1】:

隐藏第一个表单,然后关闭第二个应用程序可以停止吗?像这样:

(使用form1上的按钮打开form2,然后隐藏form1,如果form2关闭,form1也关闭);

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2 form2 = new Form2();
            form2.ShowDialog();
            this.Close();

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多