Is there a way to auto-hide a always-on-top modele

2019-07-28 17:17发布

问题:

My C# Winforms app has an always-on-top modeless find dialog. Since the user has access to the parent window while the modeless find dialog is open they can choose to open a modal dialog as well. Since the modeless dialog is always-on-top it obscures the modal dialog, but it is not possible for the user to close the modeless dialog at this point since the modal dialog locks out user interaction to all other windows.

Currently I'm working around this by manually adding code to close the modeless find dialog before opening any modal window, I'm wondering if there is a better solution?

回答1:

How about making the modeless dialog not-always-on-top before invoking modal dialogs, then restoring it?

modeless.TopMost = false;
// show modal dialog here
modeless.TopMost = true;


回答2:

Does the find dialog need to be always on top? If the purpose just is to have it floating above the main form, isn't it sufficiant to set the main form as parent to the find dialog? Then it will always be in front without have to be always-on-top.

Otherwise I'd make the find dialog listen to what happens in the app and take steps accordingly. (This can be solved in numerous ways, from events to a commong base form for all forms in the app to a CBT-hook that listens system-wide for window activation/deactivation.)