c# - 如何检查是否从另一个表单 C# 调用表单
问题描述
我有Form1
并且以这种形式,我有这个代码
Form2 frm = new Form2();
frm.ShowDialog();
所以现在是我的问题:如何知道是否form2
像
在 Button 事件中那样被调用form2
?
在按钮事件中form2
,我想检查是否ShowDialog()
被称为FROM FORM1 (NOT FROM ANOTHER FORM),如果单击按钮,form2
则关闭!
解决方案
您可以使用Form.Owner 属性。
表格1:
Form2 frm = new Form2();
frm.ShowDialog(this); // owner parameter
表格2:
if (this.Owner != null)
{
// Owner is not null, there is a calling form
// Do something
if (this.Owner is Form1)
{
Form1 form1 = (Form1)this.Owner; // Form1 called this form!
}
}
推荐阅读
- swiftui - 跨多个视图和内部数据数组的@State 变量 - Observables、Environment Published?
- c# - vstest.console.exe 不返回任何退出代码
- ios - iOS React 本机升级构建问题
- python - 重定向当前正在运行的 python 进程的标准输出
- c# - 间歇性 Azure Blob 上传失败 - 412 ConditionNotMet
- sql - SQL Server 视图性能问题
- command-line - 如何从 Minizic 输出中删除最后两行?
- javascript - 有没有办法从 cdn 导入中使用 pug.render() 函数?
- python - Keras 中的动态 RNN:使用自定义 RNN 单元在每个时间步跟踪其他输出
- python - 更改壁纸的自动脚本中的错误 - Windows 10