winform 关闭主程序,弹出唯一窗口

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 关闭主程序_弹出唯一窗口
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "zhangsan" && textBox2.Text == "123")
            {
                Form2 f2 = new Form2(this);
                f2.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("输入有误");
            }
        }
    }
}
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 关闭主程序_弹出唯一窗口
{
    public partial class Form2 : Form
    {
        Form1 F1 = null;
        public Form2(Form1 f1)
        {
            InitializeComponent();
            F1 = f1;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            F1.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        List<Form> list = new List<Form>();
        private void button1_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3(this);
            bool hasForm = false;
            foreach (Form f in list)
            {
                if (f.Name == f3.Name)//说明已经有这个窗体存在
                {
                    hasForm = true;//记录一下这个窗体已经存在的
                    f.WindowState = FormWindowState.Normal;
                    f.Show();
                    f.Activate();//激活并赋予焦点
                    //f.Focus();//焦点进入,把焦点定位在已经存在的窗体上
                    
                    f3.Close();//把新new出来的窗体关闭。
                }
            }
            if (hasForm==false)//如果没有打开,就重新打开。
            {
                f3.Show();
                list.Add(f3);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
                  Form3 f4 = new Form3(this);
            bool hasForm = false;
            foreach (Form f in list)
            {
                if (f.Name == f4.Name)//说明已经有这个窗体存在
                {
                    hasForm = true;//记录一下这个窗体已经存在的
                    f.WindowState = FormWindowState.Normal;
                    f.Show();
                    f.Activate();//激活并赋予焦点
                    //f.Focus();//焦点进入,把焦点定位在已经存在的窗体上
                    
                    f4.Close();//把新new出来的窗体关闭。
                }
            }
            if (hasForm==false)//如果没有打开,就重新打开。
            {
                f4.Show();
                list.Add(f4);
            }
        }

        public void DeleteF3(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 关闭主程序_弹出唯一窗口
{
    public partial class Form3 : Form
    {
        Form2 F2 = null;
        public Form3(Form2 f2)
        {
            InitializeComponent();
            F2 = f2;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            F2.DeleteF3(this);

        }
    }
}

 

posted on 2016-07-04 16:03  爱意红沉  阅读(250)  评论(0编辑  收藏  举报