When displaying a status message to the user, I don't like those messages that appear and disappear on the screen so fast you can't read them. I think that's poor form, and so I wrote a very simple classed derived from
System.Windows.Form to display a modeless message for a controlled minimum amount of time. The code is ridiculously simple, and if you want to embellish it with anything more sophisticated, I'd suggest taking a look at Nish's MessageBoxManager.
MessageForm class is derived from the
System.Windows.Form class, so you can customize it just as any other form. The
MessageForm will display indefinitely until one of the following occurs:
- The application calls
EndShow. The form will continue to display for the remaining required display time.
- The application calls
CancelShow. The form will be closed immediately.
- The user closes the form.
The only thing the form displays is a centered
Label docked to the extents of the form. If you want anything more interesting, such as adding a progress bar, you can change the
MessageForm in the Visual Studio designer to suit your needs.
MessageForm class implements the following properties:
Sets/gets the minimum display time for the form.
Sets/gets the message text, which is basically just a wrapper to setting the
Text property of the form's
There are three constructors:
- A default parameterless constructor
- A constructor that takes only the message text as a parameter. The form caption will be blank unless set by the application before calling one of the
- A constructor that takes the message text and caption text.
public void BeginShow()
expires = DateTime.Now.AddMilliseconds(minTime);
public void BeginShow(int minDisplayTime)
expires = DateTime.Now.AddMilliseconds(minDisplayTime);
The parameterless method shows the form, and uses the current
MinTime value to determine the minimum display time for the form. There is a second
BeginShow method that lets you override the
public void EndShow()
TimeSpan remTime = expires - DateTime.Now;
int remms = (int)remTime.TotalMilliseconds;
if (remms > 0)
This method will not close the form until the minimum display time has expired. One important thing to note is that it is a blocking call, so your thread (such as the main application thread) will be suspended until the minimum display time has expired. This probably isn't ideal, but it's a complicated issue to deal with (not from an implementation point of view, but from the perspective of usability and user feedback) and is best left to the specific application requirements.
public void CancelShow()
CancelShow is really there just for consistency in the API. It will immediately close the form.
If you call
BeginShow as part of a long process that is occurring on the main application thread, you'll need to call
Application.DoEvents immediately after the
BeginShow for the form to properly display.
Don't forget that Windows likes to manipulate a window on the same thread that it was created on. Ideally, I would create the form on the main application thread and close it there as well, which may require wrapping the
EndShow calls within a
This is a very simple component, but I find it very re-usable and useful in the applications I write. I usually don't need anything too fancy and if I do, it's so application specific, but I still use this code as the starting point.