配置主窗体并创建新窗口c#

时间:2013-01-16 10:20:59

标签: c# forms

  

可能重复:
  Windows Forms: Change application mainwindow at runtime

我想创建新表单,并在c#中销毁主表单。 我怎么能这样做?

var fw2 = new Form2();
fw2.Show();
this.Dispose();

此代码只显示第二个表单一秒钟并关闭我的程序。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我在Program类中做过这类事情。我只是将以下在Main方法中常见的代码放入循环中。

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());

一旦主窗体关闭,代码将离开'Run'方法并进入循环,该循环检查应用程序全局变量以查看是否需要呈现新表单。

如果你只是想打开一个新的表单,那就太过分了,但是如果你经常“交换”你的主要表格,它似乎工作得很好。

我的代码看起来(有点像)......

while (true)
{
    if (AppSettings.SomeFormSettng = FormSetting.ShowAnotherForm)
    {
        Form showThisForm = AppSettngs.TheForm;
        if (ThisIsTheFirstRun)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ThisIsTheFirstRun = false;
        }
        Application.Run(new showThisForm ());
    }
    else
    {
        return;
    }
}