Disable "close box (X button)" on child forms
-
Hi, how do I disable "close box" on child forms, but leave it functional for "MDI form". I tried:
private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}it works for child forms, but it also prevents me from closing apllication (with X button) from main MDI form.
-
Hi, how do I disable "close box" on child forms, but leave it functional for "MDI form". I tried:
private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}it works for child forms, but it also prevents me from closing apllication (with X button) from main MDI form.
The code you have shown is correct. What you need to do is: When the user closes your main form, you must notify the child forms that they are allowed to close. Your code could look something like this;
private bool m_bAllowClose = false;
private AllowClose(bool bAllow)
{
m_bAllowClose = bAllow;
}private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(!m_bAllowClose)
e.Cancel = true;
}From your main form you would need to call "AllowClose(true);" for all your child forms
-
The code you have shown is correct. What you need to do is: When the user closes your main form, you must notify the child forms that they are allowed to close. Your code could look something like this;
private bool m_bAllowClose = false;
private AllowClose(bool bAllow)
{
m_bAllowClose = bAllow;
}private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(!m_bAllowClose)
e.Cancel = true;
}From your main form you would need to call "AllowClose(true);" for all your child forms
On which event from main MDI form should I call AllowClose()? If I call it from:
private void MDIMainForm_FormClosed(object sender, FormClosedEventArgs e)
OR
private void MDIMainForm_FormClosing(object sender, FormClosedEventArgs e)
ChildForm_FormClosing() is validated first and it does not close child form.
-
On which event from main MDI form should I call AllowClose()? If I call it from:
private void MDIMainForm_FormClosed(object sender, FormClosedEventArgs e)
OR
private void MDIMainForm_FormClosing(object sender, FormClosedEventArgs e)
ChildForm_FormClosing() is validated first and it does not close child form.
-
Ok,if this is not working for you then you can try using the following code in your childs:
private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(e.CloseReason != CloseReason.MdiFormClosing)
e.Cancel = true;
} -
Hi, how do I disable "close box" on child forms, but leave it functional for "MDI form". I tried:
private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}it works for child forms, but it also prevents me from closing apllication (with X button) from main MDI form.
You might also want to look at Disable the Close box on a form[^] which might help with the visible "X" button - I haven;t tried it with MDI children, but it should still work.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Ok,if this is not working for you then you can try using the following code in your childs:
private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(e.CloseReason != CloseReason.MdiFormClosing)
e.Cancel = true;
}