using System.Threading;
//打开新窗体方法
new Form2().Show();//只是打开新窗体,表示第二个窗体是第一个窗体的一部分
//此时关闭第一个窗体第二个窗体也会关闭
//打开新窗体,并且关闭旧窗体
//方法一,采用线程的方法
Thread t1 = new Thread(delegate () { new Form2().ShowDialog(); });
t1.Start();
Close();
//关闭也可以使用
//Dispose();
方法二采用一个标记
//在程序启动类中定义一个标记
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp4
{
static class Program
{
public static int mark = 0;//等于0时是窗体一未关闭
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
if(mark ==1){
Application.Run(new Form2());
}
}
}
}
然后在Form1中关闭后更新mark的值
Program.mark = 1;
Close();