Click here to Skip to main content
12,354,543 members (65,965 online)
Click here to Skip to main content
Add your own
alternative version

Stats

38.9K views
328 downloads
45 bookmarked
Posted

Message Form - A minimum time display form

, 14 Dec 2006
Rate this:
Please Sign up or sign in to vote.
Display useful messages for a minimum duration.

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

/// <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

/// <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

/// <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.

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

Share

About the Author

Marc Clifton
United States United States
Marc is the creator of two open source projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.

Marc lives in Philmont, NY.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
manoj kumar choubey2-Mar-12 22:56
membermanoj kumar choubey2-Mar-12 22:56 
QuestionPut Application.DoEvents in BeginShow? Pin
Richard_N14-Dec-06 10:01
memberRichard_N14-Dec-06 10:01 
AnswerRe: Put Application.DoEvents in BeginShow? Pin
Marc Clifton14-Dec-06 11:17
protectorMarc Clifton14-Dec-06 11:17 
GeneralRe: Put Application.DoEvents in BeginShow? Pin
David Hay19-Dec-06 11:12
memberDavid 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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160621.1 | Last Updated 14 Dec 2006
Article Copyright 2006 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid