tags:

views:

1770

answers:

5

How to disable a parent form when child form is active in c# ?

+12  A: 

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.

Martinho Fernandes
This works pretty well.
Fábio Antunes
+2  A: 

Hi,

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

HTH

Phil'

Philip
+2  A: 

Check out the difference between modal and modeless :-) this should solve your troubles!

Modal and Modeless Windows Forms from Microsoft

Basically: ShowDialog() makes the ParentForm unreachable until the child form is closed.

ThePower
A: 

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);
Fredrik Mörk
A: 

How do I connect to a server that is online using windows C# Application.Because I've mostly connected to a server that is local, that is my laptop.

Marvin