Mr T
Mr T

Reputation: 524

How can I refer to an Owner of a Windows Form?

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

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);

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");
        }

I also tried this cast:

TheGameForm gForm = this.Owner as TheGameForm;

The gForm object is null! How is that possible?

Upvotes: 1

Views: 1608

Answers (1)

Jeroen van Langen
Jeroen van Langen

Reputation: 22073

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

Upvotes: 7

Related Questions