Referening a parent form control from within a child form.

K

King Coffee

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 ?

Thanks,
King
 
P

Peter Duniho

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
 
M

manas

Let's say i have button1 on the parent form.

Do this:

private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
frmChild frmC = new frmChild();
frmC.MdiParent = this;
frmC.FormClosed += new FormClosedEventHandler
(frmC_FormClosed);
frmC.Show();
}

void frmC_FormClosed(object sender, FormClosedEventArgs e)
{
button1.Enabled = true;
}

You can see that, after closing the child form, the button is enabled
again.
 
K

King Coffee

Thanks, the code worked great. However, I had to remove the "frmC.MdiParent
= this;" line.

Best regards,
King
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top