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

C#Winforms

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Solution 3 - C#

Its simple, use

Form.ShowDialog();

Instead of

Form.Show();

While using Form.ShowDialog(), you cannot interact with the parent form until it closes.

Solution 4 - C#

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

Solution 5 - C#

While using the previously mentioned childForm.ShowDialog(this) will disable your main form, it still doesent look very disabled. However if you call Enabled = false before ShowDialog() and Enable = true after you call ShowDialog() the main form will even look like it is disabled.

var childForm = new Form();
Enabled = false;
childForm .ShowDialog(this);
Enabled = true;

Solution 6 - C#

ChildForm child = new ChildForm();
child.Owner = this;
child.Show();

// In ChildForm_Load:

private void ChildForm_Load(object sender, EventArgs e) 
{
  this.Owner.Enabled = false;
}

private void ChildForm_Closed(object sender, EventArgs e) 
{
  this.Owner.Enabled = true;
} 

source : http://social.msdn.microsoft.com/Forums/vstudio/en-US/ae8ef4ef-3ac9-43d2-b883-20abd34f0e55/how-can-i-open-a-child-window-and-block-the-parent-window-only

Solution 7 - C#

Form1 frmnew = new Form1();
frmnew.ShowDialog();

Solution 8 - C#

@Melodia

Sorry for this is not C# code but this is what you would want, besides translating this should be easy.

FORM1

Private Sub Form1_MouseEnter(sender As Object, e As EventArgs) Handles MyBase.MouseEnter
    Me.Focus()
    Me.Enabled = True
    Form2.Enabled = False
End Sub

Private Sub Form1_MouseLeave(sender As Object, e As EventArgs) Handles MyBase.MouseLeave
    Form2.Enabled = True
    Form2.Focus()
End Sub

FORM2

Private Sub Form2_MouseEnter(sender As Object, e As EventArgs) Handles MyBase.MouseEnter
    Me.Focus()
    Me.Enabled = True
    Form1.Enabled = False
End Sub

Private Sub Form2_MouseLeave(sender As Object, e As EventArgs) Handles MyBase.MouseLeave
    Form1.Enabled = True
    Form1.Focus()
End Sub

Hope this helps

Solution 9 - C#

You can do that with the following:

Form3 formshow = new Form3();

formshow.ShowDialog();

Solution 10 - C#

You can also use MDIParent-child form. Set the child form's parent as MDI Parent

Eg

child.MdiParent = parentForm;
child.Show();

In this case just 1 form will be shown and the child forms will come inside the parent. Hope this helps

Solution 11 - C#

For me this work for example here what happen is the main menu will be disabled when you open the registration form.

 frmUserRegistration frmMainMenu = new frmUserRegistration();
    frmMainMenu.ShowDialog(this);

Solution 12 - C#

If you are just trying to simulate a Form.ShowDialog call but WITHOUT blocking anything (kinda like a Simulated Dialog Form) you can try using Form.Show() and as soon as you show the simulated dialog form then immediately disable all other windows using something like...

private void DisableAllWindows()
{
foreach (Form f in Application.OpenForms)
if (f.Name != this.Name)f.Enabled = false;
else f.Focus();
}

Then when you close your "pseudo-dialog form" be sure to call....

private void EnableAllWindows()
{
foreach (Form f in Application.OpenForms) f.Enabled = true;
}

Solution 13 - C#

Why not just have the parent wait for the child to close. This is more than you need.

// Execute child process
System.Diagnostics.Process proc = 
    System.Diagnostics.Process.Start("notepad.exe");
proc.WaitForExit();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvanyView Question on Stackoverflow
Solution 1 - C#R. Martinho FernandesView Answer on Stackoverflow
Solution 2 - C#PhilipView Answer on Stackoverflow
Solution 3 - C#Rizwan AnsariView Answer on Stackoverflow
Solution 4 - C#Fredrik MörkView Answer on Stackoverflow
Solution 5 - C#helgeheldreView Answer on Stackoverflow
Solution 6 - C#stackView Answer on Stackoverflow
Solution 7 - C#senthilkumar2185View Answer on Stackoverflow
Solution 8 - C#Don Thomas BoyleView Answer on Stackoverflow
Solution 9 - C#Manish sharmaView Answer on Stackoverflow
Solution 10 - C#NishaView Answer on Stackoverflow
Solution 11 - C#user3856440View Answer on Stackoverflow
Solution 12 - C#SnowMan50View Answer on Stackoverflow
Solution 13 - C#electriacView Answer on Stackoverflow