简体   繁体   English

取消TabContainer中的选项卡更改后,不会触发按钮事件

[英]Button events don't fire after canceling a tab change in a TabContainer

I've run in to an odd problem that I'm hoping you wonderful people can help me with. 我遇到了一个奇怪的问题,希望您能得到我的帮助。

I have a windows forms application in .NET 4.5. 我在.NET 4.5中有一个Windows窗体应用程序。 Contained in the main form, I have a split container. 在主窗体中,我有一个拆分容器。 The left pane contains a TreeView, while the right pane contains a TabContainer with several tabs. 左窗格包含TreeView,而右窗格包含具有多个选项卡的TabContainer。

On the first of these tabs is a docked panel that is a simple form containing two text boxes and two buttons (Save and Cancel). 这些选项卡中的第一个是停靠面板,它是一个简单的窗体,包含两个文本框和两个按钮(保存和取消)。 I have TextChanged event handlers on both text boxes that enable the buttons, and a handler on Validating for the panel. 我在两个启用按钮的文本框中都有TextChanged事件处理程序,在“为面板验证”上有一个处理程序。

The idea is that when one of the TextChanged events fires, the panel is marked "dirty", and any attempt to change focus outside the panel displays an error message asking you to either save or cancel the changes and the Validating handler sets e.Cancel to true. 这个想法是当TextChanged事件之一触发时,面板被标记为“脏”,并且任何试图在面板外部改变焦点的尝试都会显示一条错误消息,要求您保存或取消更改,并且Validating处理程序设置e.Cancel真实。

In most cases, this works fine. 在大多数情况下,这可以正常工作。 Here are some cases: 这是一些情况:

  • Change some text and click Cancel - everything behaves correctly 更改一些文本,然后单击“取消”-一切正常
  • Change some text and click Save - everything behaves correctly 更改一些文本,然后单击“保存”-一切正常
  • Change some text and click somewhere inside the TreeView - everything behaves correctly. 更改一些文本,然后单击TreeView内的某处-一切正常。 A message box is displayed and clicking either Save or Cancel will allow you to move on. 将显示一个消息框,单击“保存”或“取消”将使您继续前进。
  • Change some text and click on any other tab - this is where it gets screwy . 更改一些文本,然后单击其他任何选项卡- 这就是问题所在 A message box is displayed. 显示一个消息框。 After dismissing the message box, neither the Save or Cancel buttons will fire their Click events anymore. 关闭消息框后,“保存”或“取消”按钮都不会再触发其Click事件。 Because of this, trying to change focus to any other control (or even close the application) results in the message box being displayed again. 因此,尝试将焦点更改为任何其他控件(甚至关闭应用程序)都会导致再次显示消息框。 You're essentially stuck. 您本质上被卡住了。

I put breakpoints in the TabControl.Selected handler to make sure it wasn't firing before the Validating handler canceled the action, and it doesn't. 我在TabControl.Selected处理程序中放置了断点,以确保在Validating处理程序取消该操作之前不会触发该断点,并且不会触发。 Breakpoints in the Click handlers for the button are never hit. 按钮的Click处理程序中的断点永远不会命中。 I'm running out of ideas. 我的想法不多了。

Has anyone seen anything like this before? 有人看过这样的东西吗?

Maybe your messagebox does/did just that: change focus. 也许您的消息框只是这样做:改变焦点。

Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. 不要尝试在Enter,GotFocus,Leave,LostFocus,Validating或Validated事件处理程序中设置焦点。 Doing so can cause your application or the operating system to stop responding. 这样做可能会导致您的应用程序或操作系统停止响应。 For more information, see the WM_KILLFOCUS topic in the "Keyboard Input Reference" section, and the "Message Deadlocks" section of the "About Messages and Message Queues" topic in the MSDN library at http://msdn.microsoft.com/library . 有关更多信息,请参见http://msdn.microsoft.com/library上 MSDN库中“键盘输入参考”部分中的WM_KILLFOCUS主题和“关于消息和消息队列”主题中“消息死锁”部分。 。

MSDN on Panel.Validating Panel.Validating上的MSDN

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM