Hi,
I created two forms classes, a parent and child. When I load the child
form I want to disable a button in the parent form and then re-enable it
in the child's formclosed event handler. So, how do I reference the
parent form control objects ?
The parent form has to be written to allow this. You can do it a variety
of ways, but generally you should avoiding exposing the control itself.
Instead, just provide some public class member the child can use to
accomplish the behavior. For example, a property:
class Parent : Form
{
private Button button1;
// Use a better name than the "for example only" one I've provided
here
public bool Button1Enabled
{
get { return button1.Enabled; }
set { button1.Enabled = value; }
}
}
Of course, the child will need a reference to the parent. You can either
pass that in, to the constructor for example, set it with a property, etc.
or just look at the child's owner window and cast that to the parent type.
That said, in your short description there's nothing that suggests that
the child window really ought to be in control of the enabled/disabled
behavior anyway. Generally speaking, a parent form might want to change
its state in response to changes in the state of the child. Rather than
having the child manage the parent's state, it would make more sense for
the child to provide events for the purpose of notifying subscribers (such
as the parent form) of those changes.
For some kinds of state, you would need to implement a new event. But for
this particular example, it seems like the parent could simply disable its
own button upon showing the child form, subscribe to the child form's
FormClosed event, and then re-enable the button in the handler for that
event. The child form need not ever even know that the parent has a
button being disabled and re-enabled.
Pete