Child form code access to parent code object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I'm sorry for such a basic question, but here it is: I'm creating a VS2005
C# forms application. In the parent form I have a button that opens a child
form. In the code for the parent form I have some public objects and some
public methods. How can the child code access those objects and methods? I
know that the child has access to the parent properties by using this.Parent,
but the only members I see in the parent are the very same properties that
are available when laying out the form in the GUI designer (AcceptButton,
AutoScroll, etc.). Are the publics somehow also available using this
mechanism (or how should it be done)?

Thanks
 
You can cast "Parent" to the actual type, e.g.

MyParentType parent = this.Parent as MyParentType;
if(parent != null) {
// have access to parent.MyFunkyData
}

Alternatively, the parent can hand the resources to the child (via
properties etc) when creating it. If the intention is to update the
parent form's UI, then this can often be better done with events.

Marc
 
Back
Top