C#为什么form.Close()不会关闭的形式? [英] C# Why does form.Close() not close the form?

查看:347
本文介绍了C#为什么form.Close()不会关闭的形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下伪代码一个按钮单击事件处理程序:

I have a button click event handler with the following pseudo code:

private void btnSave_Click(object sender, EventArgs e)
{
  if(txt.Text.length == 0)
     this.Close();
  else
     // Do something else

  // Some other code...
}

这仅仅是一些简单的代码,但问题是,当文本的长度为零,我想关闭表单。但是,而不是关闭窗体的代码执行//其它一些代码的一部分。 click事件处理程序执行完毕后,再关闭表单。

This is just some simple code, but the point is, when the text length equals zero, I want to close the form. But instead of closing the form the code executes the part // Some other code. After the click event handler is completely executed, then the form is closed.

我知道,当我把收益右后 this.Close()的形式将关闭,但我想知道为什么表单没有照片直接关闭,当你调用此.Close()。为什么执行事件处理程序的其他部分?

I know, when I place return right after this.Close() the form will close, but I'd like to know WHY the form isn't direclty closed when you call this.Close(). Why is the rest of the event handler executed?

推荐答案

事件处理程序的其他部分,因为你没有离开该方法被执行。它是这么简单。

The rest of the event handler is executed because you did not leave the method. It is as simple as that.

调用 this.Close()不会立即删除的形式(和当前的事件处理)。该表格​​将在稍后被垃圾收集器,如果有到窗体的更多引用收集。

Calling this.Close() does not immediately "delete" the form (and the current event handler). The form will be collected later on by the garbage collector if there are no more references to the form.

this.Close()只不过是一个普通的方法调用,除非该方法抛出一个异常,你会留在你目前的方法的情况下。

this.Close() is nothing than a regular method call, and unless the method throws an exception you will stay in the context of your current method.

这篇关于C#为什么form.Close()不会关闭的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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