public class Hourglass : IDisposable
{
#region "Class Variables"
private Cursor _originalCursor;
private Form _parentForm;
private HourglassDialog _workingDialog;
#endregion
#region "Constructors"
/// <summary>
/// Null construct, pass in null to the secondary constuctor
/// </summary>
public Hourglass()
: this( null )
{
}
/// <summary>
/// Basic constructor will open/show the hourglass Dialog
/// </summary>
/// <param name="message">The text to display to the user</param>
public Hourglass( string message )
{
// Grab the parent form in order to open the dialog with the correct reference
ParentForm = Form.ActiveForm;
// Create and open the new hourglass dialog
WorkingDialog = new HourglassDialog();
WorkingDialog.StartPosition = FormStartPosition.CenterScreen;
if ( message != null ) { WorkingDialog.Message = message; }
// Attempt to set the curosr if we have a form.
// this will be reset later
if ( ParentForm != null )
{
OriginalCursor = ParentForm.Cursor;
ParentForm.Cursor = Cursors.WaitCursor;
WorkingDialog.Show( ParentForm );
}
else
{
WorkingDialog.Show();
}
WorkingDialog.Refresh();
}
#endregion
#region "Private Methods"
/// <summary>
/// Will close down the dialog _parentForm
/// </summary>
private void HideForm()
{
if ( WorkingDialog != null )
{
WorkingDialog.Close();
WorkingDialog = null;
}
}
/// <summary>
/// Swap the cursor out for the original/passed in cursor.
/// </summary>
private void ResetCursor()
{
if ( ParentForm != null )
{
ParentForm.Cursor = OriginalCursor;
}
}
#endregion
#region "Private Properties"
private Form ParentForm
{
get { return _parentForm; }
set { _parentForm = value; }
}
private Cursor OriginalCursor
{
get { return _originalCursor; }
set { _originalCursor = value; }
}
private HourglassDialog WorkingDialog
{
get { return _workingDialog; }
set { _workingDialog = value; }
}
#endregion
#region IDisposable Members
public void Dispose()
{
HideForm();
ResetCursor();
}
#endregion
}