Preventing an MDI child from being closed

  • Thread starter Thread starter Iain
  • Start date Start date
I

Iain

I would like to prevent the user from closing MDI child windows. However, I
would like them to be able to minimize and maximize them.

Setting the Control Box to false removes the minimize and maximize too.

An obvious thing to do is to handling the CLosing event and cancel it.
Sadly, if you do this the main Form refuses to shut down...

A solution to this is to intercept the Closing event on the main form and
use it to set a boolean that the children can check to see if they CAN close
down.

But this is raised *after* the childrens events.

Hmmm.

Any one know how I can sort this out?

Iain
 
Iain said:
I would like to prevent the user from closing MDI child windows. However, I
would like them to be able to minimize and maximize them.

Setting the Control Box to false removes the minimize and maximize too.

An obvious thing to do is to handling the CLosing event and cancel it.
Sadly, if you do this the main Form refuses to shut down...

A solution to this is to intercept the Closing event on the main form and
use it to set a boolean that the children can check to see if they CAN close
down.

But this is raised *after* the childrens events.

As a user I would be offended by you inhibiting me from closing the MDI
child. If you don't want me to close a child, the close option should be
disabled. It would be wrong to offer the close option, accept the command
and than fail to comply.

MDI has practically been deprecated for some time now, Microsoft does not
encourage to use it.

In an MDI application, all MDI Windows should contain documents of the same
type. If you are presenting different types in different MDI windows, MDI
does not apply and it should not be used.

If you are showing singleton documents/objects of different types in MDI
child windows and you were to ignore my valuable advise to not abuse the MDI
model, you could make the result less annoying on the user by accepting the
close command and hiding the child instead of closing it. The MDI window's
View menu could them offer a list of views that can be checkmarked in order
to make them visible or hide them. Close on a child would be the shortcut
for going into the menu and hiding it.

Martin.
 
Thanks for your comments, Martin.

The MDI format suits the application I have built which has a number of
'management consoles'. I don't much care if MS do not approve! (I could
argue if pressed that they are each simply a view of the underlying
database).

There appears no way of disabling only the close box. Certainly not without
disabling the minimize and maximize.

Even if I picked a hide rather than close option, I suspect I would still be
unable to close the application. After all to hide the window I would have
to cancel the close which seems to have the effect of cancelling the main
window close.

Any other thoughts?

Iain
 
You could always hide all the controls since you can't hide the X only. Then put buttons on your form to make it minimize and maximize.
 
Yeah.

I was kind of hoping for something a little less stressful.

What I've decided to do is to remove the control box completely from 2 of my
forms (which really really need to be there all the time) and have the rest
available from the File Open menu (exception that Open when a window is
present simply restores it).

Bit naughty, but it works!

Thanks for the suggestion.

Iain
Eric said:
You could always hide all the controls since you can't hide the X only.
Then put buttons on your form to make it minimize and maximize.
 
The MDI format suits the application I have built which has a number of
'management consoles'. I don't much care if MS do not approve! (I could
argue if pressed that they are each simply a view of the underlying
database).
There appears no way of disabling only the close box. Certainly not without
disabling the minimize and maximize.

Well... Maybe no .NET way. But since you are such a bad boy, not caring
about anything, you might as well do it through Win32. Here is the MFC way
to disable the close command (any menu options will automatically follow).

void CMainFrame::DisableClose()
{
CMenu* pMenu = GetSystemMenu(false);
pMenu->EnableMenuItem(SC_CLOSE, MF_DISABLED | MF_GRAYED);
}

void CMainFrame::EnableClose()
{
CMenu* pMenu = GetSystemMenu(false);
pMenu->EnableMenuItem(SC_CLOSE, MF_ENABLED);
}

Here a CMenu is used but this doesn't do much more than passing on the call
to a Win32 API function. It won't be hard to change the code to use WIn32
only. I won't help you doing it, I feel guilty enough as it is by providing
this information.
Even if I picked a hide rather than close option, I suspect I would still be
unable to close the application. After all to hide the window I would have
to cancel the close which seems to have the effect of cancelling the main
window close.

Any other thoughts?

No, still the same thought. You are abusing MDI and you are being served
right by the trouble you are getting yourself into doing so. Design
guidelines are not only there to protect the user, most of the time they
also protect the developer.

Really, it's for your own good :-).

Martin.
 
Back
Top