79

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

Spontifixus
  • 6,570
  • 9
  • 45
  • 63
  • Can't you just use ChildForm.show then Mainform.disable / .hide or are you talking about having 2 forms active at same time and when child form is ( active to disable the main ) then on (unfocus) re-activate the mainforM? – Don Thomas Boyle Aug 19 '13 at 14:29
  • @DonThomasBoyle: hey man, i'm looking for exactly your described solution "having 2 forms active at same time and when child form is ( active to disable the main ) then on (unfocus) re-activate the mainforM" can you give me a hint how to solve that? i've tried it with threading, application.run, etc. but nothing works. how would you realize this? – メロディア Sep 19 '13 at 13:05

14 Answers14

195

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.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • doesn't this hide the parent form? – barlop Jul 03 '16 at 13:46
  • @barlop to hide the parent form use `this.Hide()` in the parent form code. – vee Mar 12 '17 at 04:33
  • @vee You misread me. I was asking why was it seeming to hide it(when I didn't wanted it to). My issue, resolved, was this http://stackoverflow.com/questions/38171607/c-sharp-winforms-how-can-i-get-the-child-form-in-front-of-parent-form-without – barlop Mar 12 '17 at 19:39
  • Thanks @R. Martinho Fernandes, this works perfectly fine! – Jemil Oyebisi Apr 10 '18 at 14:28
39

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.

Spontifixus
  • 6,570
  • 9
  • 45
  • 63
Philip
  • 923
  • 7
  • 19
  • Works for me! VB.Net version is this: `Public childForm As New Childformclass` and `childForm.ShowDialog(Me)` where Me is the class parent (parent form) thank you so much @Philip – Ignacio Bustos Mar 31 '14 at 16:00
18

Its simple, use

Form.ShowDialog();

Instead of

Form.Show();

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

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Rizwan Ansari
  • 419
  • 9
  • 17
11

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
  • 155,851
  • 29
  • 291
  • 343
  • You can also handle Form_Closed instead of Form_Deactivate, if you want to disable the parent form for the lifetime of the child form, regardless of other forms or applications being activated in the mean time. – JamesFaix Feb 23 '16 at 16:25
8

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;
helgeheldre
  • 1,081
  • 11
  • 20
6
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

stack
  • 262
  • 2
  • 10
4
Form1 frmnew = new Form1();
frmnew.ShowDialog();
senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36
4

@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

Don Thomas Boyle
  • 3,055
  • 3
  • 32
  • 54
  • thanks alot! your idea is great! i just found out that it also works by working with form-parents and docking children to them. – メロディア Sep 19 '13 at 14:16
4

You can do that with the following:

Form3 formshow = new Form3();

formshow.ShowDialog();
Ctrl S
  • 1,053
  • 12
  • 31
Manish sharma
  • 526
  • 4
  • 14
2

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

Nisha
  • 1,379
  • 16
  • 28
1

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

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;
}
SnowMan50
  • 7
  • 2
0

1st way

MyChildForm childForm = new MyChildForm();
childForm.ShowDialog(this);

2nd set the parent form enable to False

Parent.Enabled = false;

Make sure to set it to true when child form close

Yılmaz Durmaz
  • 2,374
  • 12
  • 26
-2

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();
Mario S
  • 11,715
  • 24
  • 39
  • 47
electriac
  • 29
  • 1
  • 1
    -1: Don't you ever dare to do that in click/press/mouse/key handlers. Info: http://stackoverflow.com/questions/470256/process-waitforexit-asynchronously – quetzalcoatl Aug 13 '13 at 08:13
  • This answer is about PROCESSES, not about FORMS. Useful to someone, but does not address the question. Its also a really bad idea, if done on the GUI thread -- your app GUI will be locked up until the other process returns -- if anything goes wrong, user will have to resort to task manager to kill your app. USE WITH CAUTION! Or even better: use some ASYNCHRONOUS solution, rather than WaitForExit. – ToolmakerSteve Jun 25 '14 at 00:05