简体   繁体   English

如何检测从子窗体打开的模态窗体? (C# & VS2017)

[英]How to detect that a modal form is opened from a child form? (C# & VS2017)

Maybe it's a silly question but I can't find a solution.也许这是一个愚蠢的问题,但我找不到解决方案。

I have an MDI form with multiple children.我有一个有多个孩子的 MDI 表格。 There is one that I could say is the main one.我可以说有一个是主要的。

To detect that the principal is no longer used and another child form is used I use the 'Leave' event which works very well.为了检测不再使用主体并使用另一个子表单,我使用了效果很好的“离开”事件。

The problem is when from the mdi form, a modal type (.ShowDialog ()) is executed, the Leave event doesn't happen in the child form.问题是当从 mdi 表单执行模式类型 (.ShowDialog ()) 时,子表单中不会发生 Leave 事件。

Any suggestion or comment on how to get it is welcome.欢迎任何关于如何获得它的建议或评论。

you can use this to just use the current class which is in your situation the (main).您可以使用this来仅使用当前的 class ,这在您的情况下是(主)。

I solved it this way:我是这样解决的:

From the MDI form, from where the modal forms are called, I search for the child forms and if it is the "main" one, I call a public function that does the same as the Leave event of the "main" form:从调用模态 forms 的 MDI 表单中,我搜索子 forms,如果它是“主”表单,我调用公共 function 事件,它与“离开”事件的“主”事件相同:

 foreach (Form childform in this.MdiChildren) { if (childform.Name.Equals("MyMainForm")) { var formMain = (MyMainForm)childform; formMain.stopTimer(); } }

It may be a cumbersome solution, but it is functional.这可能是一个繁琐的解决方案,但它很实用。

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

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