Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the parent form when a child form is active?

Tags:

c#

winforms

People also ask

How do you pass data from child to parent in Windows Forms?

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.

Which is the correct code to add child form in parent form?

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);