“部分模态” MDI 应用程序中的表单

发布于 2024-11-08 14:04:45 字数 353 浏览 0 评论 0原文

我有一个 MDI 应用程序。用户可以打开不同的非模态表单,例如表单 A 和表单 B。表单 A 上有一个按钮可以打开另一个表单(比如说表单 C),表单 B 也是如此(一个按钮打开表单 D)。我想要实现的目标是让C阻止A,D阻止B。 ShowDialog 不起作用,因为它阻止了整个应用程序,所以我尝试禁用表单 A,然后在布尔值上循环一段时间,当 C 关闭时该布尔值设置为 false (即: while (blocked) Application.DoEvents() ;)。在下面的代码行中,我重新启用 A,然后使用从 C 获得的结果。 这似乎可行,但如果我从 A 打开 C,然后从 B 打开 D,则 A 会被 C 和 D 阻止,而不仅仅是 D 被阻止。 有没有办法实现我想要做的事情?

I've got an MDI application. The user can open different non modal forms, for instance form A and form B. On form A there is a button which opens another form (let's say form C) and the same goes for form B (a button opening form D). What I'm trying to achieve is to have C blocking A and D blocking B.
ShowDialog doesn't work because it blocks the whole app, so I tried to disable form A, and then loop with a while on a boolean which is set to false when C is closed (i.e.: while (blocked) Application.DoEvents();). In the following lines of code I re-enable A and then use the result obtained from C.
It seems to work, but if I open C from A and then D from B, A is blocked by both C and D and not only by D.
Is there a way to achieve what I'm trying to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

擦肩而过的背影 2024-11-15 14:04:46

也许这样模拟你想要的会更好:

  1. 当A打开C时,将该引用保留在A类中(不在某个方法内,但对其他方法可见)
  2. 当表单A收到Activate事件时,然后检查C是否是可见的 - 如果是,请将其聚焦。

BD 也类似,无论什么组合......

Maybe it would be better to simulate what you want like this:

  1. When A opens C, keep that reference in the A class (not inside some method, but to be visible to other methods)
  2. When form A receives Activate event, then check if C is visible - if yes, focus it instead.

Similar goes for B-D, and whatever combination...

御守 2024-11-15 14:04:46

您可以使用 Form.Activate 来强制某种形式。例如,当 D 阻止 B 并且用户选择(激活/将焦点设置到)B 时,您可以在 D 上调用 Form.Activate。这不是完全模态的,但会强制执行表单到表单的关系。当然,当没有D来阻止B时,你就不会调用Activate。这种方法还可以消除使用 DoEvents 的需要因为没有表单会真正被锁定。

You could use Form.Activate to force a sort of modality. For example, when D is blocking B and the user selects (activates/sets focus to) B you could call Form.Activate on D. It's not quite modal but would enforce your Form-to-Form relationships. Of course when there is no D to block B you would not call Activate. This approach would also rid of the need to use DoEvents since no Form would ever really be locked.

美男兮 2024-11-15 14:04:46

最后是这样解决的:
在A的方法中打开CI
1.禁用表单A
2.创建C的一个实例(如果我在C关闭后需要它的一些数据,则将其存储在A的字段中)
3. 将A类中定义的方法cClosed附加到C实例的FormClosed事件
4.打开表格C

在方法 cClosed I 中:
1. 重新启用表单 A
2. C 关闭后做我需要做的事情
3.清除包含C的字段(如果存在)

In the end solved it this way:
in the method of A that opens C I
1. disable form A
2. create an instance of C (and store it in a field of A if I will need some of its data after C has been closed)
3. attach a method cClosed defined in class A to event FormClosed of the instance of C
4. open form C

In the method cClosed I:
1. reenable form A
2. do whatever I need after the closing of C
3. clear the field containing C (if present)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文