How to disable a parent form when child form is active using 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
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
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;
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;
}
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.
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);