[英]How to open Form2 and close Form1 in the same time by C#?
I have a problem trying to open form 2 from form 1 and close form 1, and I have tried these solutions and can't find the right one: Solution 1:-我在尝试从表单 1 打开表单 2 并关闭表单 1 时遇到问题,我尝试了这些解决方案但找不到正确的解决方案:解决方案 1:-
Form2 form2 = new Form2();
form2.Show();
this.Close(); //close Form1
Solution 2:- in file Program.cs解决方案 2:- 在 Program.cs 文件中
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MyApplicationContext context = new MyApplicationContext();
Application.Run(context);
}
public class MyApplicationContext : ApplicationContext
{
private Form1 form1 = new Form1();
public MyApplicationContext()
{
form1 = new Form1();
form1.Show();
}
}
and in Form1.cs在 Form1.cs 中
Form1 form1 = new Form1();
form1.Show();
this.Close();
This solution works but after closing the applications, I find that it still works in the background.该解决方案有效,但关闭应用程序后,我发现它仍然在后台运行。
How can I fix this?我怎样才能解决这个问题?
you can try this code:你可以试试这段代码:
this.Hide();
Form2 form2 = new Form2();
form2.Closed += (s, args) => this.Close();
form2.Show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.