/
ReportUI.cs
executable file
·339 lines (324 loc) · 10.2 KB
/
ReportUI.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
using SchemaLite;
namespace SchemaLiteUI
{
/// <summary>
/// Summary description for ReportUI.
/// </summary>
public class ReportUI : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.PrintDialog printDialog1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public int linesPrinted = 0;
public string[] lines;
public ReportUI(DateTime starttime, DateTime endtime)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
textBox1.Text = starttime.Year+"-"+((starttime.Month<10)?"0":"")+starttime.Month+"-"+((starttime.Day<10)?"0":"")+starttime.Day;
textBox2.Text = endtime.Year+"-"+((endtime.Month<10)?"0":"")+endtime.Month+"-"+((endtime.Day<10)?"0":"")+endtime.Day;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ReportUI));
this.panel1 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.printDialog1 = new System.Windows.Forms.PrintDialog();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.textBox2);
this.panel1.Controls.Add(this.textBox1);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(512, 64);
this.panel1.TabIndex = 0;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(360, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(55, 16);
this.label2.TabIndex = 4;
this.label2.Text = "Slutdatum";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(288, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(60, 16);
this.label1.TabIndex = 3;
this.label1.Text = "Startdatum";
//
// button1
//
this.button1.Location = new System.Drawing.Point(432, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "Kontrollera";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(360, 24);
this.textBox2.MaxLength = 10;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(64, 20);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(288, 24);
this.textBox1.MaxLength = 10;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(64, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// richTextBox1
//
this.richTextBox1.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.richTextBox1.Location = new System.Drawing.Point(0, 64);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ReadOnly = true;
this.richTextBox1.Size = new System.Drawing.Size(512, 368);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(272, 440);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "Spara";
this.button2.Visible = false;
//
// button3
//
this.button3.Location = new System.Drawing.Point(352, 440);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "Skriv ut";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(432, 440);
this.button4.Name = "button4";
this.button4.TabIndex = 4;
this.button4.Text = "Stäng";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// ReportUI
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(512, 469);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.panel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "ReportUI";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Rapport";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Run the report
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e)
{
DateTime starttime;
DateTime endtime;
// Check that the dates are correct
try
{
starttime = DateTime.Parse(textBox1.Text);
}
catch
{
ErrorHandler.ShowInformation(ErrorHandler.ERR_DATE_WRONG);
this.textBox1.Focus();
this.textBox1.SelectAll();
return;
}
try
{
endtime = DateTime.Parse(textBox2.Text).AddDays(1);
}
catch
{
ErrorHandler.ShowInformation(ErrorHandler.ERR_DATE_WRONG);
this.textBox2.Focus();
this.textBox2.SelectAll();
return;
}
// Run report
Report rep;
if ( ErrorHandler.ShowError(Scheme.Instance.runReport(starttime, endtime, out rep)) )
return;
SortedList replist = rep.getReportLines();
richTextBox1.Text = "Rapport\nStartdatum: "+this.textBox1.Text+"\nSlutdatum: "+this.textBox2.Text+"\n\n";
richTextBox1.Text += "Person/Resurs Timmar Avvikelse\n\n";
// Write all reportlines on to the textbox
for ( int i=0; i < replist.Count; i++ )
{
ReportLine repline = (ReportLine)replist[replist.GetKey(i)];
richTextBox1.Text += repline.Name;
for(int o=0; o<30-repline.Name.Length;o++)
richTextBox1.Text += " ";
richTextBox1.Text += repline.Hours;
for(int o=0; o<11-repline.Hours.ToString().Length;o++)
richTextBox1.Text += " ";
richTextBox1.Text += repline.Deviation+"\n";
}
if ( replist.Count == 0 )
{
richTextBox1.Text += "Inga timmar är registrerade";
}
}
/// <summary>
/// Print the report
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, System.EventArgs e)
{
string currdir = Directory.GetCurrentDirectory();
PrintDocument pd = new PrintDocument();
printDialog1.Document = pd;
DialogResult dr = printDialog1.ShowDialog();
// Print if user choose to
if ( dr == System.Windows.Forms.DialogResult.OK )
{
pd.PrintPage +=new PrintPageEventHandler(pd_PrintPage);
pd.DocumentName = "Tidrapport";
linesPrinted = 0;
OnBeginPrint();
pd.Print();
}
Directory.SetCurrentDirectory(currdir);
}
/// <summary>
/// Prepare to print
/// </summary>
private void OnBeginPrint()
{
// Add all text into a variable with lines
char[] param = {'\n'};
if (printDialog1.PrinterSettings.PrintRange == PrintRange.Selection)
{
lines = richTextBox1.SelectedText.Split(param);
}
else
{
lines = richTextBox1.Text.Split(param);
}
int i = 0;
char[] trimParam = {'\r'};
foreach (string s in lines)
{
lines[i++] = s.TrimEnd(trimParam);
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
Brush brush = new SolidBrush(richTextBox1.ForeColor);
// Add all text to the graphics to be printed
while (linesPrinted < lines.Length)
{
e.Graphics.DrawString (lines[linesPrinted++],
richTextBox1.Font, brush, x, y);
y += 15;
if (y >= e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
else
{
e.HasMorePages = false;
}
}
}
/// <summary>
/// Close the report form
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}