Monday, October 15, 2007

Access items on the parent form from the child form

Suppose you have a session variable in your main form which you want to check in a child form to find out if the button text should be "logon" or "logoff".

In the child form, declare a variable MyParentForm:

public MainForm MyParentForm;

In the main form display the child form and pass a pointer to the main form:

ChildForm logonForm = new ChildForm();
logonForm.MyParentForm = this;
logonForm.Show();

In the child form you can now access the session variable:

if(MyParentForm.MySessionContext == null)
{
DoSomething();
}

No comments: