[英]close windows form works on a form but not on another one
I have crated a windows form application. 我创建了一个Windows窗体应用程序。
VS2012 created a form1
form to me VS2012给我创建了一个form1
表单
then I crated a second form called Main
然后我创建了另一个名为Main
表格
there is a button on form1
form to go to Main
form 在form1
表单上有一个按钮可以转到Main
表单
when I run the application using vs2012, and then I click the x
button at the top right of form form1 , the form close and I can start working on vs. but the problem is when I click the x button at the top right of the form Main , I can't start working on VS, but I need to click Stop Debugging
red button in the vs in order to be able to type in vs. 当我使用vs2012运行应用程序时,然后单击窗体form1右上角的x
按钮,该窗体关闭,我可以开始处理vs。但是问题是当我单击窗体右上角的x按钮时从Main表单开始,我无法开始在VS上工作,但是我需要在vs中单击“ Stop Debugging
红色按钮,以便能够输入vs。
so my question is when I give this application to my customers, and when the click the x button at the top right of the form, I need the application to close totally, I don't need them to go the task manager and close the application from it. 所以我的问题是,当我将此应用程序提供给我的客户时,当单击表单右上角的x按钮时,我需要完全关闭该应用程序,而无需他们去任务管理器并关闭该应用程序。从它的应用程序。
Your application starts with Form 1
, when you close it you are closing the application. 您的应用程序以Form 1
开头,当您关闭它时,您正在关闭该应用程序。 Main
is just a form, when you close it you are closing the form. Main
只是一个表单,当您关闭它时,您就是在关闭表单。 What you need to is, go to FormClosing
event of Main
form and add 您需要做的是,转到Main
表单的FormClosing
事件并添加
Application.Exit(); Application.Exit();
进入FormClosing事件并输入以下代码:
System.Envirenement.Exit(0);
What do you do with form1
when you go to Main
? 转到Main
时,对form1
做什么? If you just hide it in order to show the other, then close the other one, while just keeping form1
hidden, then form1
will naturally still be running, even if Main
is closed. 如果只是为了显示另一个而将其隐藏,然后关闭另一个,同时仅使form1
处于隐藏状态,则即使Main
已关闭, form1
也自然会在运行。
Sounds like the logical thing to do would be to go back to form1
from Main
when you close the latter - otherwise, you can close both using Application.Exit(0)
on the appropriate event. 听起来合乎逻辑的事情是,当您关闭Main
时,从Main
返回form1
;否则,可以在适当的事件上使用Application.Exit(0)
来关闭两者。
In either case, it would be easier to answer this if you had provided some actual code. 无论哪种情况,如果您提供了一些实际的代码,都将更容易回答。
Update: 更新:
You could also use System.Environment.Exit(0)
, but that might require more privileges, which might or might not affect your situation. 您还可以使用System.Environment.Exit(0)
,但这可能需要更多特权,这可能会或可能不会影响您的情况。 Again - some code would be helpful. 同样,一些代码会有所帮助。
In regard to your comments, see this answer for more info about termination options. 关于您的评论,请参阅此答案以获取有关终止选项的更多信息 。
Problem is because your Form1
form still running(hidden). 问题是因为您的Form1
表单仍在运行(隐藏)。
If you use Form1
only for open your Main
form. 如果仅将Form1
用于打开Main
窗体。 Then delete Form1
and set Main
as StartUp form in Properties of your proejct: 然后删除Form1
,然后在项目的“属性”中将Main
设置为StartUp表单:
Application -> StartUp form
After this when you close Main
form application will be closed properly 此后,当您关闭Main
窗体应用程序时,将正确关闭
If you use Form1
then you need close Form1
too after you close Main
form. 如果使用Form1
则在关闭Main
窗体后也需要关闭Form1
。
Use FormClosed
eventhandler for this purpose 为此使用FormClosed
事件处理程序
Right click on Main form -> Properties 右键单击主窗体->属性
on the Event tab choose FormClosing event 在“ 事件”选项卡上,选择“ FormClosing事件”
and write this code 并编写这段代码
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.