如何引用Windows窗体的所有者?

时间:2016-07-19 21:53:48

标签: c# winforms

Visual Studio 2013,Visual C#,Windows窗体应用程序。

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

只有当用户点击第二个按钮时,第一个弹出窗口才会弹出。

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

所以很明显TheGameForm是AfterTheGameForm的所有者。这就是我的问题... 在AfterTheGameForm类中 我想引用所有者类以使用其特定方法。我肯定知道所有者属于TheGameForm,我正在尝试施放:

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

我也试过这个演员:

TheGameForm gForm = this.Owner as TheGameForm;

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

1 个答案:

答案 0 :(得分:5)

这可能是因为你在构造函数中访问它。在构造函数之后设置Owner。试试Load活动。

相关问题