• 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);
    
            }
        }
    }
  • 相关阅读:
    【MySQL】全量+增量的备份/恢复
    【MySQL】MMM和MHA高可用架构
    【MySQL配置参数】sync_binlog和innodb_flush_log_at_trx_commit
    Redis2.8之后主从复制的流程
    docker push 出现:x509: certificate signed by unknown authority
    Ubuntu Docker版本的更新与安装
    CentOS7用阿里云Docker Yum源在线安装Docker
    Docker国内仓库和镜像
    Docker入门
    在centos和redhat上安装docker
  • 原文地址:https://www.cnblogs.com/zhangdemin/p/5640757.html
Copyright © 2020-2023  润新知