“部分模态” MDI 应用程序中的表单
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许这样模拟你想要的会更好:
BD 也类似,无论什么组合......
Maybe it would be better to simulate what you want like this:
Similar goes for B-D, and whatever combination...
您可以使用 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.
最后是这样解决的:
在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)