Click here to Skip to main content
15,883,896 members
Articles / Desktop Programming / Windows Forms
Article

Message Form - A Minimum Time Display Form

Rate me:
Please Sign up or sign in to vote.
4.97/5 (13 votes)
14 Dec 20063 min read 54.2K   486   47   4
Display useful messages for a minimum duration

Image 1

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

C#
/// <summary>
/// Show the form with the current minimum display time.
/// </summary>
public void BeginShow()
{
  expires = DateTime.Now.AddMilliseconds(minTime);
  Show();
}

/// <summary>
/// Show the form for a minimum time.
/// </summary>
/// <param name="minDuration">The minimum display time, 
/// in milliseconds.</param>
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

C#
/// <summary>
/// Wait until the minimum display time has expired (which it may have) then
/// close the info message dialog. This is a thread blocking call.
/// </summary>
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

C#
/// <summary>
/// Immediately close the info message.
/// </summary>
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.


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey2-Mar-12 22:56
professionalManoj Kumar Choubey2-Mar-12 22:56 
QuestionPut Application.DoEvents in BeginShow? Pin
Richard_N14-Dec-06 10:01
Richard_N14-Dec-06 10:01 
AnswerRe: Put Application.DoEvents in BeginShow? Pin
Marc Clifton14-Dec-06 11:17
mvaMarc Clifton14-Dec-06 11:17 
GeneralRe: Put Application.DoEvents in BeginShow? Pin
David Hay19-Dec-06 11:12
David Hay19-Dec-06 11:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.