进行showdialog后如何访问以前的表单? [英] How to access the previous form after doing showdialog?

查看:59
本文介绍了进行showdialog后如何访问以前的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很难弄清楚这一点.

我目前有一个frmMain和一个frmLoading.

I currently have a frmMain and a frmLoading..

在显示的frmMain事件中,我正在

inside frmMain Shown event, I'm doing

frmLoading load = new frmLoading();
load.ShowDialog();

在frmLoading内部.我想访问frmMain. 我知道我可以在构造函数中传递以前的形式 但我想尽可能避免这种情况.

inside frmLoading.. I want to access frmMain.. I know I could pass the previous form inside constructor but I'd like to avoid that if possible..

以为是this.Owner或this.Parent .. nope,无法搜索"this","Form"中的所有属性.

Was thinking it would be this.Owner, or this.Parent.. nope, searched through all the properties in "this", "Form".. could not find it..

有什么主意吗?

推荐答案

避免让Winforms猜测谁应该是所有者,将其明确:

Avoid making Winforms guess who should be the owner, make it explicit:

frmLoading load = new frmLoading();
load.ShowDialog(this);

现在,一旦Load事件运行,您就可以可靠地使用Owner属性来查找所有者.如果在构造函数中需要它,则必须将其作为构造函数参数传递,这当然不是问题,也不是首选解决方案,因为它不依赖于您使用的Show重载.考虑使用事件来避免耦合.

Now you can reliably use the Owner property to find the owner as soon as the Load event runs. If you need it in the constructor then you are going to have to pass it as a constructor argument, not an issue of course and the preferred solution since it doesn't rely on the Show overload you use. Consider using events to avoid the coupling.

这篇关于进行showdialog后如何访问以前的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆