简体   繁体   English

框架设置 e.Cancel 是否为真?

[英]Is the framework setting e.Cancel to true?

I'm writing a Windows Forms app (2.0 framework), and I'm running into a strange bug.我正在编写一个 Windows Forms 应用程序(2.0 框架),我遇到了一个奇怪的错误。

In my form, when a particular exception is thrown, I have a catch block that is supposed to log the error, inform the user with a dialog box, and then close the form by calling this.Close().在我的表单中,当引发特定异常时,我有一个 catch 块,它应该记录错误,通过对话框通知用户,然后通过调用 this.Close() 关闭表单。 The first two steps work fine, but this.Close() doesn't do anything.前两个步骤工作正常,但 this.Close() 没有做任何事情。 My first thought was that I must have an event handler that is setting e.Cancel to true, but after thoroughly checking the code, I couldn't find one.我的第一个想法是我必须有一个将 e.Cancel 设置为 true 的事件处理程序,但是在彻底检查代码之后,我找不到一个。 Also, I had not overridden the form's OnCancel method.另外,我没有覆盖表单的 OnCancel 方法。

During the process of trying to track down the bug, I wrote an override for the form's OnCancel method and set a break point in it.在试图追踪错误的过程中,我为表单的 OnCancel 方法编写了一个覆盖,并在其中设置了一个断点。 At the beginning of OnCancel, e.Cancel is already set to true.在 OnCancel 开始时,e.Cancel 已经设置为 true。 If my understanding is correct, the override should run before any event handlers, so that seems to rule out the possibility of this problem being caused by an event handler that I missed somehow.如果我的理解是正确的,覆盖应该在任何事件处理程序之前运行,这样似乎排除了这个问题是由我以某种方式错过的事件处理程序引起的可能性。

In my OnClosing override method, if I set e.Cancel to false, the form closes fine, and my program works as I intended it to, but I don't want to fix it that way.在我的 OnClosing 覆盖方法中,如果我将 e.Cancel 设置为 false,则表单将正常关闭,并且我的程序按预期工作,但我不想那样修复它。 There is obviously some code that is creating an unexpected side-effect, and I want to understand what's going on rather than resorting to "digital duct tape."显然有一些代码会产生意想不到的副作用,我想了解发生了什么,而不是求助于“数字胶带”。

It seems like the framework itself is canceling my form closing.似乎框架本身正在取消我的表单关闭。 Are there any situations where the framework would do that?在任何情况下框架会这样做吗?

Thanks.谢谢。

edit...编辑...

I created a small test project and was able to recreate the error on a much simpler form.我创建了一个小型测试项目,并且能够以更简单的形式重新创建错误。

If you would like the recreate the situation so that you can see what I mean, try this... Create a form and add a textbox (I assume other controls would work too).如果您想重新创建这种情况以便您能明白我的意思,试试这个... 创建一个表单并添加一个文本框(我假设其他控件也可以工作)。 Have your form handle the text box's Validated event, and within the handler call this.Close.让您的表单处理文本框的 Validated 事件,并在处理程序中调用 this.Close。 Now override Form.OnClosing() and set a breakpoint in it.现在覆盖 Form.OnClosing() 并在其中设置断点。

Now run the program, type some text, and tab out of the textbox so it validates (I guess you'll want to add a button or something so that you have some other control to tab to.)现在运行程序,输入一些文本,然后从文本框中跳出标签,以便它验证(我猜你会想要添加一个按钮或其他东西,以便你有一些其他的控件可以使用标签。)

At the beginning of OnClosing, e.Cancel will be automatically set to true This is what I'm trying to figure out.在 OnClosing 开始时, e.Cancel 将自动设置为 true 这就是我想要弄清楚的。 Why is that happening?为什么会这样?

I have the exact same scenario in the Framework 2.0.我在 Framework 2.0 中有完全相同的场景。 If a certain set of events occur, then the following code will stop at a break point on 3rd line of this sub every time thereafter regardless of what else happens in the app.如果发生了某组事件,那么此后的代码每次都会在该 sub 的第 3 行的断点处停止,而不管应用程序中还发生了什么。

This seems to happen after we disable all of the controls on a form and then enable all of those same controls and then close that form (we are locking that form down as another process changes some related data).这似乎发生在我们禁用表单上的所有控件,然后启用所有这些相同的控件,然后关闭该表单之后(我们正在锁定该表单,因为另一个进程更改了一些相关数据)。

If this is the lowest method in the system for the application form closing event then what else can be setting e.Cancel to true except the framework?如果这是系统中申请表关闭事件的最低方法,那么除了框架之外,还有什么可以将 e.Cancel 设置为 true 呢?

Protected Overrides Sub OnFormClosing(e As System.Windows.Forms.FormClosingEventArgs)
  If e.Cancel Then
    e.Cancel = False
  End If
  MyBase.OnFormClosing(e)
End Sub

I'm not sure if you are asking us to guess if your code is correct or what exactly.我不确定您是否要求我们猜测您的代码是否正确或究竟是什么。 I can tell you that I've never seen this problem.我可以告诉你,我从未见过这个问题。

However, it is painfully easy to determine if some event handler is canceling: just use the debugger.但是,确定某个事件处理程序是否正在取消非常容易:只需使用调试器即可。 Step through the code and see what happens.单步执行代码,看看会发生什么。

Given the tiny amount of details you've provided here, I think your focus should be on figuring out what is happening.鉴于您在此处提供的少量细节,我认为您的重点应该是弄清楚发生了什么。

You question is quite unclear.你的问题很不清楚。 You refer to a method, OnCancel , however no such method exists in the Form class.您引用了一种方法OnCancel ,但是在 class Form中不存在这种方法。 You are using .NET 2.0 according to the first sentence in your question, however you are overriding Form.OnClosing .根据问题中的第一句话,您正在使用 .NET 2.0,但是您正在覆盖Form.OnClosing Note that this method is deprecated as of version 2.0 of the framework.请注意,从框架的 2.0 版开始,此方法已被弃用。 You should be using Form.OnFormClosing instead, new as of 2.0.您应该改用Form.OnFormClosing ,从 2.0 开始新。 Please try overriding that method instead and see if you observe the same issue.请尝试改写该方法,看看您是否发现相同的问题。 I have certainly never seen such a thing from OnFormClosing .我当然从未在OnFormClosing中看到过这样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# WinForms 表单在 Form_FormClosing 事件中设置 e.Cancel = true 后仍然关闭 - c# WinForms form still closes after setting e.Cancel = true in Form_FormClosing event e.cancel = true之后,后台工作者将不会运行“ RunWorkerCompleted”; - Background worker will not run “RunWorkerCompleted” after e.cancel = true; 在Validating事件中使用e.Cancel = true的其他方法还有哪些? - What are other alternatives to using e.Cancel = true in Validating event? 在封闭事件中的“e.Cancel” - “e.Cancel ” in formclosing Event e。取消不起作用 - e.Cancel does not work 如果e.Cancel = true,我何时何地应该调用base.OnClosing(e)? - If e.Cancel = true, when and where should I call base.OnClosing(e)? WPF DataGrid,如果在 CellEditEnding 事件中 e.Cancel = true,则将焦点放在单元格上 - WPF DataGrid, Keep Focus on cell if e.Cancel = true in CellEditEnding event 无法编辑DataGridView-cell,验证事件集e.Cancel = true - Cannot edit DataGridView-cell, Validating-event sets e.Cancel = true 在行验证中执行e.Cancel = true后,在radDataGridView内的GridViewDateTimeColumn中进行Esc - Esc in GridViewDateTimeColumn inside radDataGridView after I did an e.Cancel = true in Row Validation C# Wpf App e.Cancel = true 无法使用! 寻找保持应用程序在托盘中运行 - C# Wpf App e.Cancel = true unable to use! looking for keep application running in tray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM