Introduction
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.
Implementation
The 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.
The API
Properties
The MessageForm
class implements the following properties:
MinTime
Sets/gets the minimum display time for the form.
MessageText
Sets/gets the message text, which is basically just a wrapper to setting the Text
property of the form's Label
control.
Constructors
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
BeginShow
methods. - A constructor that takes the message text and caption text
Methods
BeginShow
public void BeginShow()
{
expires = DateTime.Now.AddMilliseconds(minTime);
Show();
}
public void BeginShow(int minDisplayTime)
{
expires = DateTime.Now.AddMilliseconds(minDisplayTime);
Show();
}
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 MinTime
value.
EndShow
public void EndShow()
{
TimeSpan remTime = expires - DateTime.Now;
int remms = (int)remTime.TotalMilliseconds;
if (remms > 0)
{
Thread.Sleep(remms);
}
Close();
}
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.
CancelShow
public void CancelShow()
{
Close();
}
CancelShow
is really there just for consistency in the API. It will immediately close the form.
Notes
Calling DoEvents
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.
Using Form.Invoke
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 BeginShow
and EndShow
calls within a Form.Invoke
delegate.
Conclusion
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.
History
- 14th December, 2006: Initial version
License
This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.