c#点击按钮跳转到另一窗口后将本窗口关闭

Form1是主窗口.Form2是子窗口.
实现点击按钮后弹出Form2.然后主窗口关闭.退出Form2时主窗口再弹出来供其他操作.

1.主窗口不能关闭,只能影藏,给form1设置焦点,当窗体激活时发生

                Form2 myform = new Form2();             
                myform.Show();
                this.Opacity = 0;

 

        private void Form1_Activated(object sender, EventArgs e)
        {
            this.Opacity = 100;
        }

2.传递值:

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

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Opacity = 0;
            Form2 f2 = new Form2();
            f2.ShowDialog();
            this.Opacity = f2.ss;
        }
    }
}

namespace WindowsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public int ss
        {
            get
            {
                return 100;
            }
        }
    }
}

3.C#关闭窗口时把之前隐藏的主窗口一起关闭:


Application.Exit();

 

 

4.在按钮后弹出Form2.然后主窗口进行隐藏.退出Form2时主窗口再显示出来

Form2 fm2 = new Form2();
fm2.Show();
// 不在任务栏显示 
this.ShowInTaskbar = false;
// 窗体透明 
this.Opacity = 0; //后面的取值0~100 ,0表示透明

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页