Repository navigation
Expand file tree
/
Copy pathMainForm.cs
More file actions
349 lines (302 loc) · 13.6 KB
/
Copy pathMainForm.cs
File metadata and controls
349 lines (302 loc) · 13.6 KB
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
340
341
342
343
344
345
346
347
348
349
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections.Generic;
namespace DriverDatabase
{
public class MainForm : Form
{
public ListBox ListBox;
public Button AddButton;
public Button EditButton;
private MainMenu mainMenu;
private IContainer components;
private Label Folder;
private MenuStrip menuStrip1;
private ToolStripMenuItem fileToolStripMenuItem;
private Button button1;
public Button DeleteButton;
private ToolStripMenuItem clearToolStripMenuItem;
private ToolStripMenuItem saveToolStripMenuItem;
private Object selectedItem;
private List<ToolStripItem> itemList;
private ToolStripMenuItem viewToolStripMenuItem;
private List<Object> objectList;
public MainForm()
{
InitializeComponent();
DriverList.Instance.ListChanged += new EventHandler(this.OnListChanged);
CarList.Instance.ListChanged += new EventHandler(this.OnListChanged);
Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.ListBox = new System.Windows.Forms.ListBox();
this.AddButton = new System.Windows.Forms.Button();
this.EditButton = new System.Windows.Forms.Button();
this.DeleteButton = new System.Windows.Forms.Button();
this.mainMenu = new System.Windows.Forms.MainMenu(this.components);
this.Folder = new System.Windows.Forms.Label();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.clearToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.button1 = new System.Windows.Forms.Button();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
// ListBox
this.ListBox.ItemHeight = 20;
this.ListBox.Location = new System.Drawing.Point(37, 53);
this.ListBox.Name = "ListBox";
this.ListBox.Size = new System.Drawing.Size(262, 144);
this.ListBox.TabIndex = 0;
this.ListBox.SelectedIndexChanged += new System.EventHandler(this.OnListBoxSelectedIndexChanged);
// AddButton
this.AddButton.Location = new System.Drawing.Point(315, 53);
this.AddButton.Name = "AddButton";
this.AddButton.Size = new System.Drawing.Size(142, 50);
this.AddButton.TabIndex = 1;
this.AddButton.Text = "Add driver";
this.AddButton.Click += new System.EventHandler(this.OnAddDriverButtonClick);
// EditButton
this.EditButton.Enabled = false;
this.EditButton.Location = new System.Drawing.Point(315, 136);
this.EditButton.Name = "EditButton";
this.EditButton.Size = new System.Drawing.Size(142, 50);
this.EditButton.TabIndex = 2;
this.EditButton.Text = "Edit";
this.EditButton.Click += new System.EventHandler(this.OnEditButtonClick);
// DeleteButton
this.DeleteButton.Location = new System.Drawing.Point(477, 136);
this.DeleteButton.Name = "DeleteButton";
this.DeleteButton.Size = new System.Drawing.Size(142, 50);
this.DeleteButton.TabIndex = 3;
this.DeleteButton.Text = "Delete";
this.DeleteButton.Click += new System.EventHandler(this.OnDeleteButtonClick);
// Folder
this.Folder.AutoSize = true;
this.Folder.Location = new System.Drawing.Point(38, 29);
this.Folder.Name = "Folder";
this.Folder.Size = new System.Drawing.Size(59, 18);
this.Folder.TabIndex = 4;
this.Folder.Text = "Drivers:";
// menuStrip1
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem,
this.viewToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(639, 28);
this.menuStrip1.TabIndex = 5;
this.menuStrip1.Text = "menuStrip1";
// fileToolStripMenuItem
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.clearToolStripMenuItem,
this.saveToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(46, 24);
this.fileToolStripMenuItem.Text = "Data";
// clearToolStripMenuItem
this.clearToolStripMenuItem.Name = "clearToolStripMenuItem";
this.clearToolStripMenuItem.Size = new System.Drawing.Size(126, 26);
this.clearToolStripMenuItem.Text = "Clear";
this.clearToolStripMenuItem.Click += new System.EventHandler(this.clearToolStripMenuItem_Click);
// saveToolStripMenuItem
this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(126, 26);
this.saveToolStripMenuItem.Text = "Save";
this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
// viewToolStripMenuItem
this.viewToolStripMenuItem.Name = "viewToolStripMenuItem";
this.viewToolStripMenuItem.Size = new System.Drawing.Size(55, 24);
this.viewToolStripMenuItem.Text = "View";
this.viewToolStripMenuItem.Click += new System.EventHandler(this.viewToolStripMenuItem_Click);
viewToolStripMenuItem_Click(null, null);
// button1
this.button1.Location = new System.Drawing.Point(477, 53);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(142, 50);
this.button1.TabIndex = 6;
this.button1.Text = "Add car";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.OnAddCarButtonClick);
// MainForm
this.ClientSize = new System.Drawing.Size(639, 211);
this.Controls.Add(this.button1);
this.Controls.Add(this.Folder);
this.Controls.Add(this.ListBox);
this.Controls.Add(this.AddButton);
this.Controls.Add(this.EditButton);
this.Controls.Add(this.DeleteButton);
this.Controls.Add(this.menuStrip1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MainMenuStrip = this.menuStrip1;
this.Menu = this.mainMenu;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Closed += new System.EventHandler(this.OnFormClosed);
this.Load += new System.EventHandler(this.OnFormLoad);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private void OnListChanged(object sender, EventArgs e)
{
this.EditButton.Enabled = false;
this.ListBox.Items.Clear();
if (selectedItem is String || selectedItem is null)
{
Folder.Text = "Drivers:";
for (int i = 0; i < DriverList.Instance.Count; i++)
{
this.ListBox.Items.Add(DriverList.Instance[i]);
}
}
else if (selectedItem is Driver)
{
Driver selectedDriver = (Driver)selectedItem;
Folder.Text = selectedDriver.Name + ":";
for (int i = 0; i < selectedDriver.Cars.Count; i++)
{
this.ListBox.Items.Add(selectedDriver.Cars[i]);
}
}
}
private void OnAddDriverButtonClick(object sender, EventArgs e)
{
DriverForm driverForm = new DriverForm();
if (driverForm.ShowDialog() == DialogResult.OK)
{
DriverList.Instance.Add(driverForm.Driver);
}
}
private void OnAddCarButtonClick(object sender, EventArgs e)
{
CarForm bookForm = new CarForm();
if (bookForm.ShowDialog() == DialogResult.OK)
{
CarList.Instance.Add(bookForm.Car);
}
}
private void OnEditButtonClick(object sender, EventArgs e)
{
if (this.ListBox.SelectedItem == null) return;
if (this.ListBox.SelectedItem is Driver)
{
DriverForm driverForm = new DriverForm();
driverForm.Driver = this.ListBox.SelectedItem as Driver;
driverForm.ShowDialog();
}
else if (this.ListBox.SelectedItem is Car)
{
CarForm carForm = new CarForm();
carForm.Car = this.ListBox.SelectedItem as Car;
bool[] checkedDrivers = new bool[DriverList.Instance.Drivers.Count];
for (int index = 0; index < DriverList.Instance.Drivers.Count; index++)
{
foreach (var driver in carForm.Car.Drivers)
{
if (DriverList.Instance.Drivers[index] == driver)
checkedDrivers[index] = true;
}
}
carForm.CheckedDrivers = checkedDrivers;
carForm.ShowDialog();
}
}
private void OnDeleteButtonClick(object sender, EventArgs e)
{
if (this.ListBox.SelectedItem == null) return;
if (this.ListBox.SelectedItem is Driver)
DriverList.Instance.Remove(this.ListBox.SelectedItem as Driver);
else if (this.ListBox.SelectedItem is Car)
CarList.Instance.Remove(this.ListBox.SelectedItem as Car);
}
private void OnListBoxSelectedIndexChanged(object sender, System.EventArgs e)
{
bool selected = this.ListBox.SelectedItem != null;
this.EditButton.Enabled = selected;
}
private void OnFormLoad(object sender, System.EventArgs e)
{
try
{
DriverList.Instance.LoadFromFile(DriverList.DefaultFileName);
CarList.Instance.LoadFromDrivers();
}
catch (System.IO.FileNotFoundException)
{
DriverList.Instance.CreateFile(DriverList.DefaultFileName);
DriverList.Instance.SaveToFile(DriverList.DefaultFileName);
}
catch (Exception ex)
{
MessageBox.Show("Failed to load DriverList: " + ex.ToString());
}
}
private void OnFormClosed(object sender, System.EventArgs e)
{
}
private void OnApplicationExit(object sender, EventArgs e)
{
try
{
DriverList.Instance.SaveToFile(DriverList.DefaultFileName);
}
catch (Exception ex)
{
MessageBox.Show("Failed to save DriverList: " + ex.ToString());
}
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
CarList.Instance.Cars = new List<Car>();
DriverList.Instance.Drivers = new List<Driver>();
DriverList.Instance.SaveToFile(DriverList.DefaultFileName);
DriverList.Instance.LoadFromFile(DriverList.DefaultFileName);
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
DriverList.Instance.SaveToFile(DriverList.DefaultFileName);
}
private void viewToolStripMenuItem_Click(object sender, EventArgs e)
{
itemList = new List<ToolStripItem>();
ToolStripItem firstItem = new ToolStripMenuItem
{
Size = new System.Drawing.Size(126, 26),
Text = "All drivers"
};
firstItem.Click += new System.EventHandler(this.viewMenu_Click);
itemList.Add(firstItem);
objectList = new List<object>();
objectList.Add("All drivers");
int count = 1;
foreach (var driver in DriverList.Instance.Drivers)
{
objectList.Add(driver);
ToolStripItem item = new ToolStripMenuItem
{
Name = count.ToString(),
Size = new System.Drawing.Size(126, 26),
Text = driver.Name
};
item.Click += new System.EventHandler(this.viewMenu_Click);
itemList.Add(item);
count++;
}
viewToolStripMenuItem.DropDownItems.Clear();
viewToolStripMenuItem.DropDownItems.AddRange(itemList.ToArray());
}
private void viewMenu_Click(object sender, EventArgs e)
{
string toParse = (sender as ToolStripItem) != itemList[0] ? (sender as ToolStripItem).Name : "0";
selectedItem = objectList[int.Parse(toParse)];
DriverList.Instance.FireListChanged();
}
}
}