To send data from Parent to Child form you need to create a parameterized constructor. To send data from Child to Parent form you need to create a public method in Child class and use the method in Parent class to get the return value.
Try assigning the parent form of your first child from: Form2 f2 = new Form2; f2.
Have you tried using Form.ShowDialog() instead of Form.Show()?
ShowDialog shows your window as modal, which means you cannot interact with the parent form until it closes.
Are you calling ShowDialog()
or just Show()
on your child form from the parent form?
ShowDialog
will "block" the user from interacting with the form which is passed as a parameter to ShowDialog
.
Within the parent you might call something like:
MyChildForm childForm = new MyChildForm();
childForm.ShowDialog(this);
where this
is the parent form.
Its simple, use
Form.ShowDialog();
Instead of
Form.Show();
While using Form.ShowDialog()
, you cannot interact with the parent form until it closes.
What you could do, is to make sure to pass the parent form as the owner when showing the child form:
Form newForm = new ChildForm();
newForm.Show(this);
Then, in the child form, set up event handlers for the Activated
and Deactivate
events:
private void Form_Activated(object sender, System.EventArgs e)
{
if (this.Owner != null)
{
this.Owner.Enabled = false;
}
}
private void Form_Deactivate(object sender, System.EventArgs e)
{
if (this.Owner != null)
{
this.Owner.Enabled = true;
}
}
However, this will result in a truly wierd behaviour; while you will not be able to go back and interact with the parent form immediately, activating any other application will enable it, and then the user can interact with it.
If you want to make the child form modal, use ShowDialog
instead:
Form newForm = new ChildForm();
newForm.ShowDialog(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With