在C#2017中打开其他表单时无法获取打开表单 [英] Cannot Get Open Form to close when opening other form in C# 2017

查看:67
本文介绍了在C#2017中打开其他表单时无法获取打开表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

        private Form2 f2 = null;
        private Form3 f3 = null;

        private void Form2button_Click(object sender, EventArgs e)
        {
            if (this.f2 == null)
            {
                f2 = new Form2();
                f2.FormClosed += F2_FormClosed;
                f2.Show();
            }
            else
            {
                f2.BringToFront();
            }

        }

        private void F2_FormClosed(object sender, FormClosedEventArgs e)
        {
            f2 = null;
        }

        private void Form3Button_Click(object sender, EventArgs e)
        {
            if (this.f3 == null)
            {
                f3 = new Form3();
                f3.FormClosed += F3_FormClosed;
                f3.Show();
            }
            else
            {
                f3.BringToFront();
            }
        }

        private void F3_FormClosed(object sender, FormClosedEventArgs e)
        {
            f3 = null;
        }
    }
}


如果Form3处于打开状态,我想在单击Form2Button_Click时关闭此表单。当我点击Form3Button_Click时,Form2也是如此。
$


有人可以告诉我该怎么做,因为我无法弄清楚这一点。



Garth

If Form3 is open I would like to close this form when I click on Form2Button_Click. Same applies with Form2 when I click on Form3Button_Click.

Can someone please tell me how to do this as I cannot figure this out.

Garth

推荐答案


尝试这样做...

Try to do this...

{
    this.Hide();
    Form1 sistema = new Form1();
    sistema.ShowDialog();
    this.Close();
}





这篇关于在C#2017中打开其他表单时无法获取打开表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆