Pseudo System Modal Dialog
May 11th, 2007
VB programmers with even a little experience know that a modal dialog prevents the user from doing anything else in their application. But, what if you had a situation where you didn’t want the user to take any other action with any other application when your modal dialog was being displayed?
In 16-bit Windows 3.x a programmer could invoke a system modal window that prevented any other processes from having control of the system until the user closed it. This concept went away with the arrival of Windows 95, NT 3.51, and their 32-bit descendants.
In general, it was not a good programming practice to use a system modal dialog but there are some circumstances when it is needed. This need most often comes up in dedicated systems, such as point-of-sale, industrial monitoring or other dedicated systems, where there may be multiple applications running but one needs all of the user’s attention at a critical time.
This example shows how to implement a pseudo system modal dialog in an application. It isn’t fool proof and won’t be effective on a dual monitor system or in other situations/configurations. The dialog won’t prevent the user from invoking and using Task Manager, if they have access to it, and users can use their Windows keyboard button to push the Windows taskbar back to the top if they have the option set to keep it on top of other windows. However, in spite of these limitations, this method can be effective way to get a user’s attention on a critical task.
This pseudo system modal dialog works with two forms, a cover form and the actual dialog form. The real trick is in the cover form that it is Maximized, has it’s TopMost property set to True, and its Opacity set to 10%. This form is used to cover up the other open windows on the system. The dialog form needs to also be shown TopMost as well and it also needs the cover form to always stay behind it. This is accomplished by showing this form using ShowDialog and disabling the cover form. I’ve also found it to be a good safety valve to have code in the cover form that always returns control to the dialog.
You can show the actual dialog as either a regular application modal dialog or you can use the overloaded ShowDialog method to display it in system modal form. The code in the overload manages showing the cover form and the FormClosing event is used to hide the cover when the dialog is done.
The sample program, written in VS 2005 VB.NET is very simple and easy to follow with full XML comments. It demonstrates showing the dialog both ways. It also demos working with a timer to show the dialog so that you can see what happens when this application does not have focus.
Be careful how you use this example. It would be easy to write a bad user interface with this technique. But, used properly and wisely, it can be a valuable addition to your VB.NET bag of tricks.
ยป Click here to download VB.NET source code for this article
Entry Filed under: Code Examples
Rate This Article:









(4 votes, average: 4 out of 5)
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed