如何引用 Windows 窗体的所有者? [英] How can I refer to an Owner of a Windows Form?

查看:20
本文介绍了如何引用 Windows 窗体的所有者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 2013、Visual C#、Windows 窗体应用程序.

Visual Studio 2013, Visual C#, Windows Form Applications.

我对两个 Form 类感兴趣.AfterTheGameForm.csTheGameForm.cs.

I am interested in two Form classes. The AfterTheGameForm.cs and the TheGameForm.cs.

只有当用户点击第二个中的按钮时,第一个才会弹出.

The first one pop's up only if the user clicks a button in the second one.

AfterTheGameForm afterTheGameForm = new AfterTheGameForm();
afterTheGameForm.Show(this);

所以很明显 TheGameForm 是 AfterTheGameForm 的所有者.这就是我的问题... 在 AfterTheGameForm 类中 我想引用 Owner 类以使用其特定方法.我确定所有者属于 TheGameForm 类型,我正在尝试投射:

So it's clear that TheGameForm is the Owner of AfterTheGameForm. And here lies my problem... In the AfterTheGameForm class I want to refer to the Owner class in order to use its specific methods. I know for sure that the Owner is of type TheGameForm and I am trying to cast:

TheGameForm gForm = (TheGameForm)this.Owner;
        if(gForm!=null){               
            MessageBox.Show(theGameForm.CheckedRadioButton);
        }
        else
        {
            MessageBox.Show("theGameForm==null");
        }

我也试过这个演员表:

TheGameForm gForm = this.Owner as TheGameForm;

gForm 对象为空!这怎么可能?

The gForm object is null! How is that possible?

推荐答案

那可能是因为您正在构造函数中访问它.Owner 在构造函数之后设置.尝试 Load 事件.

That probably because you're accessing it in the constructor. The Owner is set after the constructor. Try the Load event.

这篇关于如何引用 Windows 窗体的所有者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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