-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgressDialog.cs
128 lines (114 loc) · 3.73 KB
/
ProgressDialog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Windows.Forms;
using System.Drawing;
namespace JpegRotator
{
/// <summary>
/// The available modes of the progress dialog.
/// </summary>
public enum ProgressDialogType
{
Loading,
Saving
}
/// <summary>
/// Implements a "progress meter" dialog.
/// </summary>
public partial class ProgressDialog : Form
{
public event EventHandler CancelButtonClick;
private int _fileCount;
private ProgressDialogType _type;
/// <summary>
/// Creates a new ProgressDialog instance.
/// </summary>
/// <param name="progressDialogType">The mode of the dialog, affecting the text displayed
/// on the dialog.</param>
public ProgressDialog(ProgressDialogType progressDialogType)
{
InitializeComponent();
//Save the specified progressDialogType for use elsewhere in this class.
this._type = progressDialogType;
//Set the dialog caption based on the dialog type.
if (progressDialogType == ProgressDialogType.Loading)
{
this.Text = "Loading...";
}
else if (progressDialogType == ProgressDialogType.Saving)
{
this.Text = "Saving...";
}
else
{
throw new NotImplementedException("Unrecognized progressDialogType value: " + progressDialogType);
}
}
/// <summary>
/// Sets the count of items currently being handled by the process whose progress is
/// being displayed by the dialog.
/// </summary>
/// <param name="fileCount">The item count. </param>
public void SetFileCount(int fileCount)
{
this._fileCount = fileCount;
}
/// <summary>
/// Updates the dialog based on the specified index (the index of the item currently
/// being processed).
/// </summary>
/// <param name="currentFile">The 1-based index of the item being processed. </param>
public void UpdateProgress(int currentFile)
{
//Update the text shown on the dialog.
if (this._type == ProgressDialogType.Loading)
{
this._progressLabel.Text = "Opening file " + currentFile + " of " + this._fileCount + "...";
}
else
{
this._progressLabel.Text = "Saving file " + currentFile + " of " + this._fileCount + "...";
}
//Center the label horizontally on the dialog.
this._progressLabel.Location = new Point((this.Width / 2) - (this._progressLabel.Width / 2), this._progressLabel.Location.Y);
//Calculate the process percentage, and update the progress bar with the calculated value.
int progressPercent = (int)((((float)(currentFile - 1)) / ((float)this._fileCount)) * 100);
this._progressBar.Value = progressPercent;
}
/// <summary>
/// Activated when the Cancel button is clicked. Fires a CancelButtonClick event.
/// </summary>
/// <param name="sender">Unused. </param>
/// <param name="e">Unused. </param>
private void _cancelButton_Click(object sender, EventArgs e)
{
FireCancelEvent();
//Disable the Cancel button and change the text to indicate to the user that their
//click has been processed.
this._cancelButton.Enabled = false;
this._cancelButton.Text = "Cancelling...";
}
/// <summary>
/// Activated when the form is closed.
/// </summary>
/// <param name="sender">Unused. </param>
/// <param name="e">Unused. </param>
private void ProgressDialog_FormClosed(object sender, FormClosedEventArgs e)
{
//Treat the user closing the form via Alt+F4 or whatever other means
//as a click on the Cancel button.
FireCancelEvent();
}
/// <summary>
/// Fires a CancelButtonClick event to notify the main form that the operation in progress
/// should be cancelled (if the operation hasn't already finished).
/// </summary>
private void FireCancelEvent()
{
if (this.CancelButtonClick != null)
{
EventArgs eventArgs = new EventArgs();
this.CancelButtonClick(this, eventArgs);
}
}
}
}