关闭主进程和弹出唯一窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "feng" && textBox2.Text == "123")
            {
                Form2 f2 = new Form2(this);
                f2.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("输入有误!");
                return;
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public partial class Form2 : Form
    {
        Form1 F1;
        public Form2(Form1 f1)//窗体传值
        {
            InitializeComponent();
            F1 = f1;
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            F1.Close();//当form2关闭的时候,form1同时关闭
        }
        List<Form> list = new List<Form>();
        private void button1_Click(object sender, EventArgs e)
        {
            bool has = false;
            Form3 f3 = new Form3(this);
            f3.Owner = this;
            
            foreach(Form f in list)
            {
                if (f.Name == f3.Name)//判断一下是否有该窗体
                {
                    has = true;//如果有该窗体,记录一下
                    f.WindowState = FormWindowState.Normal;//窗体启动常规大小
                    f.Show();//已有的窗体显示
                    f3.Close();//新建的窗体关闭
                    f.Activate();//激活窗体并赋给它窗体
                }
            }

            if (has == false)//如果没有该窗体
            {
                f3.Show();//创建显示一个form3的窗体
                list.Add(f3);//添加form3窗体进入list集合中
            }
        }
        //当form3窗体关闭后,将form3窗体从list集合中移除
        public void deleteform3( Form3 f3)
        {
            List<Form> aa=new List<Form>();
            foreach(Form f in list )
            {
                if(f.Name!=f3.Name)
                {
                    aa.Add(f);
                }

            }
            list=aa;
        }




    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public partial class Form3 : Form
    {
        Form2 F2;
        public Form3(Form2 f2)
        {
            InitializeComponent();
            F2 = f2;
            
        }

        private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            F2.deleteform3(this);//移除form3窗体
        }
    }
}

原文地址:https://www.cnblogs.com/fengsantianya/p/5638634.html