-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dashboard.cs
288 lines (259 loc) · 10.6 KB
/
Dashboard.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using USPS_Barcode_System.Enums;
using USPS_Barcode_System.Business;
namespace USPS_Barcode_System
{
public partial class USPSBarcodeSystem : Form
{
public EnumActionType actionType;
public USPSBarcodeSystem()
{
InitializeComponent();
}
/// <summary>
/// Clear all the fields in the form.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void clear_Click(object sender, EventArgs e)
{
txtBoxInput.Text = "";
txtBoxOutput.Text = "";
}
/// <summary>
/// Change the font appearance of the output Bar Code.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFontStyle_Click(object sender, EventArgs e)
{
if (txtBoxOutput.Text != "")
{
DialogResult result = fontDialog.ShowDialog();
if (result == DialogResult.OK)
{
// Get Font.
Font font = fontDialog.Font;
// Set TextBox Font.
this.txtBoxOutput.Font = fontDialog.Font;
}
}
else
showErrorMessage(EnumErrorType.OutputBoxIsEmpty);
}
/// <summary>
/// Entry point for Encoding Zip Code.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void encodeZipCode_Click(object sender, EventArgs e)
{
panelConversion.Visible = true;
lblInput.Visible = true;
lblOutput.Visible = true;
txtBoxInput.Visible = true;
this.txtBoxInput.Focus();
txtBoxInput.Text = "";
txtBoxInput.MaxLength = 10;
txtBoxOutput.Visible = true;
txtBoxOutput.Text = "";
btnFontStyle.Visible = true;
btnClear.Visible = true;
btnConvert.Visible = true;
btnConvert.Text = "Encode";
pictureBoxBg.Visible = false;
lblTitle.Text = "Encode Zip Code to Bar Code";
lblInput.Text = "Enter Zip Code";
actionType = EnumActionType.EncodeZipCode;
}
/// <summary>
/// Entry point for Decoding Bar Code.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void decodeBarCode_Click(object sender, EventArgs e)
{
panelConversion.Visible = true;
lblInput.Visible = true;
lblOutput.Visible = true;
txtBoxInput.Visible = true;
txtBoxInput.Text = "";
this.txtBoxInput.Focus();
txtBoxInput.MaxLength = 52;
txtBoxOutput.Visible = true;
txtBoxOutput.Text = "";
txtBoxOutput.Font = new Font("Century Gothic, 21.75pt", 21.75f);
btnFontStyle.Visible = false;
btnClear.Visible = true;
btnConvert.Visible = true;
btnConvert.Text = "Decode";
pictureBoxBg.Visible = false;
lblTitle.Text = "Decode Bar Code to Zip Code";
lblInput.Text = "Enter Bar Code";
actionType = EnumActionType.DecodeBarCode;
}
/// <summary>
/// To adjust output textbox according to changes in the font appearance.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtBoxOutput_FontChanged(object sender, EventArgs e)
{
Size textBoxRect = TextRenderer.MeasureText(this.txtBoxOutput.Text, this.txtBoxOutput.Font,
new Size(this.txtBoxOutput.Width, int.MaxValue),
TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl);
//Adding scrollbar.
this.txtBoxOutput.ScrollBars = textBoxRect.Height > this.txtBoxOutput.Height ?
ScrollBars.Vertical :
ScrollBars.None;
}
/// <summary>
/// Entry point for Decoding Bar Code.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConvert_Click(object sender, EventArgs e)
{
switch(actionType)
{
//Encode Zip Code to Bar Code
case EnumActionType.EncodeZipCode:
if (txtBoxInput.Text.Length != 10)
showErrorMessage(EnumErrorType.InvalidZipCodeInput);
else
{
String barCode = BarCodeManager.encodeZipCode(txtBoxInput.Text);
txtBoxOutput.Text = barCode;
}
break;
//Decode Bar Code to Zip Code
case EnumActionType.DecodeBarCode:
if (txtBoxInput.Text.Length != 52)
showErrorMessage(EnumErrorType.InvalidBarCodeInput);
else
{
String zipCode = "";
EnumStatus status = BarCodeManager.decodeBarCode(txtBoxInput.Text, out zipCode);
if (status == EnumStatus.Success)
{
string sVal = zipCode;
string newst = Regex.Replace(sVal, ".{5}", "$0-");
txtBoxOutput.Text = newst;
txtBoxOutput.SelectionStart = txtBoxOutput.Text.Length;
}
else
showErrorMessage((EnumErrorType) Enum.Parse(typeof(EnumErrorType), status.ToString()));
}
break;
}
}
/// <summary>
/// To limit the user for entering specfic inputs.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtBoxInput_KeyPress(object sender, KeyPressEventArgs e)
{
switch(actionType)
{
case EnumActionType.EncodeZipCode:
e.Handled = !(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back);
break;
case EnumActionType.DecodeBarCode:
if (!char.IsControl(e.KeyChar) && (e.KeyChar != ':') && (e.KeyChar != '|'))
e.Handled = true;
break;
}
}
/// <summary>
/// To guide the user for input on mouse hover.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void picBoxHelp_MouseHover(object sender, EventArgs e)
{
PictureBox PB = null;
switch (actionType)
{
case EnumActionType.EncodeZipCode:
PB = (PictureBox) sender;
toolTipHelp.Show("Hint: Zip code must be of ZIP+4 digits", PB, -20, 30, 2000);
break;
case EnumActionType.DecodeBarCode:
PB = (PictureBox) sender;
toolTipHelp.Show("Hint: \nFull bar = '|' \nHalf bar = ':'", PB, -20, 30, 2000);
break;
}
}
/// <summary>
/// To guide the user for input on click of hint.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void picBoxHelp_Click(object sender, EventArgs e)
{
switch (actionType)
{
case EnumActionType.EncodeZipCode:
MessageBox.Show("Zip code must be of ZIP+4 digits", "Hint", MessageBoxButtons.OK, MessageBoxIcon.Question);
break;
case EnumActionType.DecodeBarCode:
MessageBox.Show("\nFull bar = '|' \nHalf bar = ':'", "Hint", MessageBoxButtons.OK, MessageBoxIcon.Question);
break;
}
}
/// <summary>
/// To dynamically seperate 5 digit zip code and 4 digit area code by adding a hyphen.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtBoxInput_KeyDown(object sender, KeyEventArgs e)
{
string sVal = txtBoxInput.Text;
if (actionType == EnumActionType.EncodeZipCode && !string.IsNullOrEmpty(sVal) && e.KeyCode != Keys.Back)
{
sVal = sVal.Replace("-", "");
string newst = Regex.Replace(sVal, ".{5}", "$0-");
txtBoxInput.Text = newst;
txtBoxInput.SelectionStart = txtBoxInput.Text.Length;
}
}
/// <summary>
/// Display apppropriate error messages for each exception scenarios.
/// </summary>
/// <param name="errorType"></param>
public static void showErrorMessage(EnumErrorType errorType)
{
switch (errorType)
{
case EnumErrorType.InvalidZipCodeInput:
MessageBox.Show("The Zip code input should be ZIP+4 digits!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case EnumErrorType.InvalidBarCodeInput:
MessageBox.Show("The Bar code input should have 52 half/full bars!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case EnumErrorType.IncorrectBarCode:
MessageBox.Show("The given Bar Code is incorrect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case EnumErrorType.IncorrectCheckDigit:
MessageBox.Show("The given Bar Code does not compute correctly against the check digit", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case EnumErrorType.OutputBoxIsEmpty:
MessageBox.Show("Output box is empty!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
private void txtBoxOutput_TextChanged(object sender, EventArgs e)
{
}
}
}