关闭c#.net中的单个表单 [英] Closing a single form in c#.net

查看:66
本文介绍了关闭c#.net中的单个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式,即form1和form2。现在我在form1上有一个按钮。单击一个按钮,我想关闭form1并显示form2。我写的代码是这样的:

I have two forms ie form1 and form2. Now I have a button on form1. On click of a button i want to close form1 and show form2. The code I have written is like this:

form2 f2=new form2()
f2.show();
this.close()



但是this.close会关闭整个应用程序。



有人可以帮帮我吗?

提前谢谢。


But this.close closes the entire application.

Could someone please help me?
Thanks in advance.

推荐答案

如果你查看Program.cs,你会看到Form1被传递给Application.Run 。



当你关闭Form1时,框架会假设你退出程序。



你可以隐藏Form1,然后显示Form2,或者您可以创建一个后台管理器并在Application.Run中使用它来控制正在使用的表单。除非你找到/制定一个合适的模式来了解哪些表格是开放的以及何时你应该关闭,否则这会感觉有点hackish。您将需要创建和管理一个ApplicationContext,当您不再打开表单时,它将自行关闭。它还应该负责创建表单。



干杯。
If you look in Program.cs, you'll see that Form1 is passed in to Application.Run.

When you close Form1, the framework assumes you're exiting the program.

You can hide Form1, then show Form2, or you can create a background manager and use that in Application.Run to control which form is being used. This will feel a bit hackish unless you find/work out a decent pattern for knowing which forms are open and when you're supposed to close. You will need to create and manage an ApplicationContext that shuts itself down when you have no more forms open. It should also be responsible for creating the forms.

Cheers.


这是因为您将Form1定义为主要表单。您可以隐藏它(form1.visible = false;)关闭它。



可以关闭form1,但你必须运行第二个表单才能获得messageloop再次出现,如下所示:

This is because you defined Form1 as your main form. You could hide it instead (form1.visible = false;) of closing it.

It is possible to close form1 but you have to run the second form to get the messageloop going again, like this:
Application.Run(new form2());





祝你好运!



Good luck!


由于form1是父类,而form2是子形式,所以不可能在父表单关闭时打开子表单。



删除了垃圾邮件链接。
As form1 is parent and form2 is child form.. so it is not possible to open a child form when a parent form is closed.

SPAM link removed.


这篇关于关闭c#.net中的单个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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