C#中打开一个新的窗体,然后关闭当前的形式? [英] c# open a new form then close the current form?

查看:888
本文介绍了C#中打开一个新的窗体,然后关闭当前的形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,假设我在表格1,然后我想:


  1. 开启形式2(从按钮的形式1)

  2. 关闭表1

  3. 专注于形式2


解决方案

史蒂夫的解决方案是行不通的。当调用this.Close(),当前的形式与窗体2一起处理。因此,你需要隐藏它,并设置form2.Closed事件调用this.Close()。

 私人无效OnButton1Click(对象发件人,EventArgs的发送)
{
    this.Hide();
    VAR窗口2 =新Form2的();
    form2.Closed + =(S,参数)=> this.Close();
    form2.Show();
}

For example, Assume that I'm in form 1 then I want:

  1. Open form 2( from a button in form 1)
  2. Close form 1
  3. Focus on form 2

解决方案

Steve's solution does not work. When calling this.Close(), current form is disposed together with form2. Therefore you need to hide it and set form2.Closed event to call this.Close().

private void OnButton1Click(object sender, EventArgs e)
{
    this.Hide();
    var form2 = new Form2();
    form2.Closed += (s, args) => this.Close(); 
    form2.Show();
}

这篇关于C#中打开一个新的窗体,然后关闭当前的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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