vany
vany

Reputation:

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

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

Upvotes: 81

Views: 131161

Answers (14)

Himanshu Sharma
Himanshu Sharma

Reputation: 1

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

Upvotes: 0

Rizwan Ansari
Rizwan Ansari

Reputation: 439

Its simple, use

Form.ShowDialog();

Instead of

Form.Show();

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

Upvotes: 19

Manish sharma
Manish sharma

Reputation: 536

You can do that with the following:

Form3 formshow = new Form3();

formshow.ShowDialog();

Upvotes: 4

SnowMan50
SnowMan50

Reputation: 7

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

Upvotes: 1

user3856440
user3856440

Reputation: 23

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

Upvotes: 1

Don Thomas Boyle
Don Thomas Boyle

Reputation: 3045

@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

Upvotes: 4

Nisha
Nisha

Reputation: 1439

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

Upvotes: 2

senthilkumar2185
senthilkumar2185

Reputation: 2566

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

Upvotes: 4

stack
stack

Reputation: 262

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

Upvotes: 6

helgeheldre
helgeheldre

Reputation: 1101

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;

Upvotes: 8

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234354

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.

Upvotes: 198

Philip
Philip

Reputation: 933

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.

Upvotes: 40

electriac
electriac

Reputation: 29

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

Upvotes: -2

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

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

Upvotes: 11

Related Questions