/
FormMain.cs
970 lines (844 loc) · 30.8 KB
/
FormMain.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SOFT152Assignment
{
public partial class FormMain : Form
{
// String containing the file path of the data file.
public string dataFile;
// The user's access level (staff or public).
public string level;
// To determine whether or not the form is open.
public bool open;
public FormMain(string accessLevel)
{
InitializeComponent();
Utils.SetIcon(this);
this.open = true;
this.level = accessLevel.ToLower();
if(this.level == "staff")
{
this.Text = "Staff Access";
}
else if(this.level == "public")
{
buttonAddDistrict.Hide();
buttonAddNeighborhood.Hide();
buttonAddProperty.Hide();
buttonEditDistrict.Hide();
buttonEditNeighborhood.Hide();
buttonEditProperty.Hide();
this.Text = "Public Access";
}
}
// Ensures the application actually closes when the form does.
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
if(this.open)
{
this.open = false;
this.Close();
Application.Exit();
}
}
// The "Activated" event is triggered whenever the form gains focus. In this case, it's used to refresh the data (if it has changed) when a popup form is closed.
private void FormMain_Activated(object sender, EventArgs e)
{
if(dataFile != null && dataFile != "")
{
if(Data.changed)
{
int indexDistrict = -1;
int indexNeighborhood = -1;
int indexProperty = -1;
if(listviewDistricts.SelectedItems.Count == 1)
{
indexDistrict = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
}
if(listviewNeighborhoods.SelectedItems.Count == 1)
{
indexNeighborhood = Convert.ToInt32(listviewNeighborhoods.SelectedItems[0].SubItems[0].Text);
}
if(listviewProperties.SelectedItems.Count == 1)
{
indexProperty = Convert.ToInt32(listviewProperties.SelectedItems[0].SubItems[0].Text);
}
PopulateList(Data.districts, "districts");
listviewNeighborhoods.Clear();
listviewProperties.Clear();
labelSelectDistrict.Visible = true;
labelSelectDistrictAndNeighborhood.Visible = true;
// Reselect the district, neighborhood, or property that the user had selected before the ListView was refreshed.
if(indexDistrict >= 0 && Data.deletedDistrict == false)
{
listviewDistricts.Items[indexDistrict].Selected = true;
listviewDistricts.EnsureVisible(indexDistrict);
}
if(indexNeighborhood >= 0 && Data.deletedNeighborhood == false)
{
listviewNeighborhoods.Items[indexNeighborhood].Selected = true;
listviewNeighborhoods.EnsureVisible(indexNeighborhood);
}
if(indexProperty >= 0 && Data.deletedProperty == false)
{
listviewProperties.Items[indexProperty].Selected = true;
listviewProperties.EnsureVisible(indexProperty);
}
Data.changed = false;
Data.unsaved = true;
Data.deletedDistrict = false;
Data.deletedNeighborhood = false;
Data.deletedProperty = false;
Utils.EnableControl(buttonSave);
}
}
}
private void ButtonBack_Click(object sender, EventArgs e)
{
listviewDistricts.Clear();
listviewNeighborhoods.Clear();
listviewProperties.Clear();
labelSelectDistrict.Visible = true;
labelSelectDistrictAndNeighborhood.Visible = true;
ShowForm(new FormAccess(), true, false);
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if(Data.unsaved)
{
StreamWriter writer = new StreamWriter(dataFile);
foreach(District district in Data.districts)
{
writer.WriteLine(district.Name);
writer.WriteLine(district.Neighborhoods.Length);
foreach(Neighborhood neighborhood in district.Neighborhoods)
{
writer.WriteLine(neighborhood.Name);
writer.WriteLine(neighborhood.Properties.Length);
foreach(Property property in neighborhood.Properties)
{
writer.WriteLine(property.Id);
writer.WriteLine(property.Name);
writer.WriteLine(property.HostID);
writer.WriteLine(property.HostName);
writer.WriteLine(property.Count);
writer.WriteLine(property.Latitude);
writer.WriteLine(property.Longitude);
writer.WriteLine(property.RoomType);
writer.WriteLine(property.RoomPrice);
writer.WriteLine(property.RoomNights);
writer.WriteLine(property.RoomAvailability);
}
}
}
writer.Close();
Data.unsaved = false;
Utils.DisableControl(buttonSave);
}
else
{
MessageBox.Show("No changes have been made to save.", "Error");
}
}
private void ButtonAnalysis_Click(object sender, EventArgs e)
{
if(dataFile != null && dataFile != "")
{
Form form = new FormAnalysis();
form.StartPosition = FormStartPosition.Manual;
form.Location = new Point(this.Left, this.Top);
form.ShowDialog();
}
}
private void PanelSearchDistrict_Click(object sender, EventArgs e)
{
// User might click on the panel instead of the TextBox. This takes care of that.
inputSearchDistrict.Focus();
}
private void InputSearchDistrict_Enter(object sender, EventArgs e)
{
// Remove placeholder text.
if(inputSearchDistrict.Text == "Search...")
{
inputSearchDistrict.Text = "";
}
}
private void InputSearchDistrict_Leave(object sender, EventArgs e)
{
// Add placeholder text.
if(inputSearchDistrict.Text.Trim() == "")
{
inputSearchDistrict.Text = "Search...";
}
}
private void InputSearchDistrict_KeyUp(object sender, KeyEventArgs e)
{
if(inputSearchDistrict.Text.Trim() == "")
{
if(this.dataFile != null && this.dataFile != "")
{
PopulateList(Data.districts, "districts");
}
Search("districts", inputSearchDistrict.Text.Trim());
}
else
{
Search("districts", inputSearchDistrict.Text.Trim());
}
}
private void ButtonAddDistrict_Click(object sender, EventArgs e)
{
// A data file is required to actually modify and add data to.
if(this.dataFile != null && this.dataFile != "")
{
ShowForm(new PopupDistrict("staff", "add"), false, true);
}
}
private void ButtonEditDistrict_Click(object sender, EventArgs e)
{
if(this.dataFile != null && this.dataFile != "")
{
if(listviewDistricts.SelectedItems.Count == 1)
{
int index = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
ShowForm(new PopupDistrict("staff", "edit", index), false, true);
}
else
{
MessageBox.Show("Select a district first.", "Error");
}
}
}
private void ButtonViewDistrict_Click(object sender, EventArgs e)
{
if(listviewDistricts.SelectedItems.Count == 1)
{
int index = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
ShowForm(new PopupDistrict("staff", "view", index), false, true);
}
else
{
MessageBox.Show("Select a district first.", "Error");
}
}
private void PanelSearchNeighborhood_Click(object sender, EventArgs e)
{
if(listviewDistricts.SelectedIndices.Count == 1)
{
// User might click on the panel instead of the TextBox. This takes care of that.
inputSearchNeighborhood.Focus();
}
}
private void InputSearchNeighborhood_Enter(object sender, EventArgs e)
{
// Remove placeholder text.
if(inputSearchNeighborhood.Text == "Search..." && listviewDistricts.SelectedIndices.Count == 1)
{
inputSearchNeighborhood.Text = "";
}
}
private void InputSearchNeighborhood_Leave(object sender, EventArgs e)
{
// Add placeholder text.
if(inputSearchNeighborhood.Text.Trim() == "" && listviewDistricts.SelectedIndices.Count == 1)
{
inputSearchNeighborhood.Text = "Search...";
}
}
private void InputSearchNeighborhood_KeyUp(object sender, KeyEventArgs e)
{
if(listviewDistricts.SelectedIndices.Count == 1)
{
if(inputSearchNeighborhood.Text.Trim() == "")
{
if(this.dataFile != null && this.dataFile != "")
{
PopulateList(Data.districts, "neighborhoods");
}
Search("neighborhoods", inputSearchNeighborhood.Text.Trim());
}
else
{
Search("neighborhoods", inputSearchNeighborhood.Text.Trim());
}
}
else
{
inputSearchNeighborhood.Text = "Search...";
}
}
private void ButtonAddNeighborhood_Click(object sender, EventArgs e)
{
// A data file is required to actually modify and add data to.
if(this.dataFile != null && this.dataFile != "")
{
if(listviewDistricts.SelectedItems.Count == 1)
{
int index = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
ShowForm(new PopupNeighborhood("staff", "add", index), false, true);
}
else
{
MessageBox.Show("Select a district to add a neighborhood to first.", "Error");
}
}
}
private void ButtonEditNeighborhood_Click(object sender, EventArgs e)
{
// A data file is required to actually modify and add data to.
if(this.dataFile != null && this.dataFile != "")
{
if(listviewDistricts.SelectedItems.Count == 1 && listviewNeighborhoods.SelectedItems.Count == 1)
{
int districtIndex = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
int neighborhoodIndex = Convert.ToInt32(listviewNeighborhoods.SelectedItems[0].SubItems[0].Text);
ShowForm(new PopupNeighborhood("staff", "edit", districtIndex, neighborhoodIndex), false, true);
}
else
{
MessageBox.Show("Select a district and a neighborhood first.", "Error");
}
}
}
private void ButtonViewNeighborhood_Click(object sender, EventArgs e)
{
if(listviewDistricts.SelectedItems.Count == 1 && listviewNeighborhoods.SelectedItems.Count == 1)
{
int districtIndex = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
int neighborhoodIndex = Convert.ToInt32(listviewNeighborhoods.SelectedItems[0].SubItems[0].Text);
ShowForm(new PopupNeighborhood("staff", "view", districtIndex, neighborhoodIndex), false, true);
}
else
{
MessageBox.Show("Select a district and a neighborhood first.", "Error");
}
}
private void PanelSearchProperty_Click(object sender, EventArgs e)
{
if(listviewDistricts.SelectedIndices.Count == 1 && listviewNeighborhoods.SelectedIndices.Count == 1)
{
// User might click on the panel instead of the TextBox. This takes care of that.
inputSearchProperty.Focus();
}
}
private void InputSearchProperty_Enter(object sender, EventArgs e)
{
// Remove placeholder text.
if(inputSearchProperty.Text == "Search..." && listviewDistricts.SelectedIndices.Count == 1 && listviewNeighborhoods.SelectedIndices.Count == 1)
{
inputSearchProperty.Text = "";
}
}
private void InputSearchProperty_Leave(object sender, EventArgs e)
{
// Add placeholder text.
if(inputSearchProperty.Text.Trim() == "" && listviewDistricts.SelectedIndices.Count == 1 && listviewNeighborhoods.SelectedIndices.Count == 1)
{
inputSearchProperty.Text = "Search...";
}
}
private void InputSearchProperty_KeyUp(object sender, KeyEventArgs e)
{
if(listviewDistricts.SelectedIndices.Count == 1 && listviewNeighborhoods.SelectedIndices.Count == 1)
{
if(inputSearchProperty.Text.Trim() == "")
{
if(this.dataFile != null && this.dataFile != "")
{
PopulateList(Data.districts, "properties");
}
Search("properties", inputSearchProperty.Text.Trim());
}
else
{
Search("properties", inputSearchProperty.Text.Trim());
}
}
else
{
inputSearchProperty.Text = "Search...";
}
}
private void ButtonAddProperty_Click(object sender, EventArgs e)
{
// A data file is required to actually modify and add data to.
if(this.dataFile != null && this.dataFile != "")
{
if(listviewDistricts.SelectedItems.Count == 1 && listviewNeighborhoods.SelectedItems.Count == 1)
{
int districtIndex = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
int neighborhoodIndex = Convert.ToInt32(listviewNeighborhoods.SelectedItems[0].SubItems[0].Text);
ShowForm(new PopupProperty("staff", "add", districtIndex, neighborhoodIndex), false, true);
}
else
{
MessageBox.Show("Select a district and neighborhood first.", "Error");
}
}
}
private void ButtonEditProperty_Click(object sender, EventArgs e)
{
// A data file is required to actually modify and add data to.
if(this.dataFile != null && this.dataFile != "")
{
if(listviewDistricts.SelectedItems.Count == 1 && listviewNeighborhoods.SelectedItems.Count == 1 && listviewProperties.SelectedItems.Count == 1)
{
int districtIndex = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
int neighborhoodIndex = Convert.ToInt32(listviewNeighborhoods.SelectedItems[0].SubItems[0].Text);
int propertyIndex = Convert.ToInt32(listviewProperties.SelectedItems[0].SubItems[0].Text);
ShowForm(new PopupProperty("staff", "edit", districtIndex, neighborhoodIndex, propertyIndex), false, true);
}
else
{
MessageBox.Show("Select a district, neighborhood, and property first.", "Error");
}
}
}
private void ButtonViewProperty_Click(object sender, EventArgs e)
{
if(listviewDistricts.SelectedItems.Count == 1 && listviewNeighborhoods.SelectedItems.Count == 1 && listviewProperties.SelectedItems.Count == 1)
{
int districtIndex = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
int neighborhoodIndex = Convert.ToInt32(listviewNeighborhoods.SelectedItems[0].SubItems[0].Text);
int propertyIndex = Convert.ToInt32(listviewProperties.SelectedItems[0].SubItems[0].Text);
ShowForm(new PopupProperty("staff", "view", districtIndex, neighborhoodIndex, propertyIndex), false, true);
}
else
{
MessageBox.Show("Select a district, neighborhood, and property first.", "Error");
}
}
private void ListviewDistricts_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
// If there aren't any selected items in the ListView, then the edit button is "grayed out". I realize the "Disabled" property exists, but it's easier to style the button this way.
if(listviewDistricts.SelectedItems.Count == 1)
{
Utils.EnableControl(buttonViewDistrict);
Utils.EnableControl(buttonEditDistrict);
Utils.EnableControl(buttonAddNeighborhood);
Utils.EnableControl(panelSearchNeighborhood);
Utils.EnableControl(inputSearchNeighborhood);
listviewNeighborhoods.Clear();
listviewProperties.Clear();
labelSelectDistrict.Visible = false;
labelSelectDistrictAndNeighborhood.Visible = true;
labelSelectDistrictAndNeighborhood.Text = "Select a Neighborhood First...";
AddColumns("neighborhoods");
int districtIndex = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
District district = Data.districts[districtIndex];
int i = 0;
foreach(Neighborhood neighborhood in district.Neighborhoods)
{
listviewNeighborhoods.Items.Add(new ListViewItem(new string[3] { i.ToString(), neighborhood.Name, neighborhood.PropertyCount.ToString() }));
i++;
}
}
else
{
Utils.DisableControl(buttonViewDistrict);
Utils.DisableControl(buttonEditDistrict);
Utils.DisableControl(buttonAddNeighborhood);
Utils.DisableControl(panelSearchNeighborhood);
Utils.DisableControl(inputSearchNeighborhood);
Utils.DisableControl(panelSearchProperty);
Utils.DisableControl(inputSearchProperty);
listviewNeighborhoods.Clear();
listviewProperties.Clear();
labelSelectDistrict.Visible = true;
labelSelectDistrict.Text = "Select a District First...";
labelSelectDistrictAndNeighborhood.Visible = true;
labelSelectDistrictAndNeighborhood.Text = "Select a District && Neighborhood First...";
}
}
private void ListviewNeighborhoods_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
// If there aren't any selected items in the ListView, then the edit button is "grayed out". I realize the "Disabled" property exists, but it's easier to style the button this way.
if(listviewNeighborhoods.SelectedItems.Count == 1)
{
listviewProperties.Clear();
labelSelectDistrictAndNeighborhood.Visible = false;
AddColumns("properties");
int neighborhoodIndex = Convert.ToInt32(listviewNeighborhoods.SelectedItems[0].SubItems[0].Text);
if(listviewDistricts.SelectedItems.Count == 1)
{
Utils.EnableControl(buttonViewNeighborhood);
Utils.EnableControl(buttonEditNeighborhood);
Utils.EnableControl(buttonAddProperty);
Utils.EnableControl(panelSearchProperty);
Utils.EnableControl(inputSearchProperty);
int districtIndex = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
District district = Data.districts[districtIndex];
Neighborhood neighborhood = district.Neighborhoods[neighborhoodIndex];
int i = 0;
foreach(Property property in neighborhood.Properties)
{
listviewProperties.Items.Add(new ListViewItem(new string[12] { i.ToString(), property.Id.ToString(), property.Name, property.HostID.ToString(), property.HostName, property.Count.ToString(), property.Longitude.ToString(), property.Latitude.ToString(), property.RoomType, property.RoomPrice.ToString(), property.RoomNights.ToString(), property.RoomAvailability.ToString() }));
i++;
}
}
}
else
{
Utils.DisableControl(buttonViewNeighborhood);
Utils.DisableControl(buttonEditNeighborhood);
Utils.DisableControl(buttonAddProperty);
Utils.DisableControl(panelSearchProperty);
Utils.DisableControl(inputSearchProperty);
listviewProperties.Clear();
labelSelectDistrictAndNeighborhood.Visible = true;
}
}
private void ListviewProperties_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
// If there aren't any selected items in the ListView, then the edit button is "grayed out". I realize the "Disabled" property exists, but it's easier to style the button this way.
if(listviewDistricts.SelectedItems.Count == 1 && listviewNeighborhoods.SelectedItems.Count == 1 && listviewProperties.SelectedItems.Count == 1)
{
Utils.EnableControl(buttonViewProperty);
Utils.EnableControl(buttonEditProperty);
}
else
{
Utils.DisableControl(buttonViewProperty);
Utils.DisableControl(buttonEditProperty);
}
}
private void FileDialog_FileOk(object sender, CancelEventArgs e)
{
// Display the file name to the user along with "Loading" so they know they chose the right file.
labelFileDialog.Text = "Loading " + fileDialog.SafeFileName;
ReadFile(fileDialog.FileName);
this.dataFile = fileDialog.FileName;
if(this.dataFile != null && this.dataFile != "")
{
Utils.EnableControl(buttonAddDistrict);
}
}
private void LabelFileDialog_Click(object sender, EventArgs e)
{
// Disable file selection if one's already been selected.
if(labelFileDialog.Text == "Select a Data Source...")
{
fileDialog.Title = "Select a Data Source File";
fileDialog.FileName = "*.txt";
// The "CurrentDirectory" returns the current "working" folder, which would be the "debug" folder.
string debugDirectory = Environment.CurrentDirectory;
// To set the initial directory of the FileDialog to the project folder.
fileDialog.InitialDirectory = Directory.GetParent(debugDirectory).Parent.FullName;
fileDialog.CheckFileExists = true;
fileDialog.CheckPathExists = true;
fileDialog.ShowDialog();
}
}
private void LabelFileDialog_MouseEnter(object sender, EventArgs e)
{
labelFileDialog.BackColor = Color.FromArgb(70, 70, 70);
}
private void LabelFileDialog_MouseLeave(object sender, EventArgs e)
{
labelFileDialog.BackColor = Color.FromArgb(60, 60, 60);
}
private void LabelFileDialog_DragEnter(object sender, DragEventArgs e)
{
labelFileDialog.BackColor = Color.FromArgb(70, 70, 70);
}
private void LabelFileDialog_DragLeave(object sender, EventArgs e)
{
labelFileDialog.BackColor = Color.FromArgb(60, 60, 60);
}
private void LabelFileDialog_DragOver(object sender, DragEventArgs e)
{
try
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
catch(Exception error)
{
MessageBox.Show(error.Message, "Error");
}
}
private void LabelFileDialog_DragDrop(object sender, DragEventArgs e)
{
try
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if(files.Length > 1)
{
MessageBox.Show("You can only read one file.", "Error");
}
else
{
// Display the file name to the user along with "Loading" so they know they chose the right file.
labelFileDialog.Text = "Loading " + files[0];
ReadFile(files[0]);
this.dataFile = files[0];
if(this.dataFile != null && this.dataFile != "")
{
Utils.EnableControl(buttonAddDistrict);
}
}
}
catch(Exception error)
{
MessageBox.Show(error.Message, "Error");
}
}
private void ShowForm(Form form, bool matchSize, bool keepOpen)
{
int screenWidth = Screen.FromControl(this).Bounds.Width;
// The "keepOpen" boolean is used to determine whether or not the current from should be kept open. Clicking something like the back button would count as closing the current form, so the "keepOpen" would be set to false. A popup, on the other hand, would constitute a situation in which the current form should ideally be kept open.
if(!keepOpen)
{
form.Show();
this.Hide();
// Open form in the same location.
form.Left = this.Left;
form.Top = this.Top;
}
else
{
int x = screenWidth - form.Width - 100;
int y = 100;
// Setting the location of a dialog is different from a normal form.
form.StartPosition = FormStartPosition.Manual;
form.Location = new Point(x, y);
// "ShowDialog" defers from the normal "Show" method, because it essentially disables the current form while the new one is open. This is good for a popup, because the user interacting with the data while a popup is open could lead to issues.
form.ShowDialog();
}
// Most forms have the same size, but some (such as popups) might not.
if(matchSize)
{
form.Size = this.Size;
}
}
private void Search(string category, string query)
{
if(this.dataFile != null && this.dataFile != "")
{
int queryLength = query.Length;
PopulateList(Data.districts, category);
ListView.ListViewItemCollection items = listviewDistricts.Items;
if(category == "neighborhoods")
{
items = listviewNeighborhoods.Items;
}
else if(category == "properties")
{
items = listviewProperties.Items;
}
if(query == "" || query == "Search...")
{
// If the search query is empty, then all ListViewItems are restored. I wanted to use "Hide()" on them originally, but that doesn't seem to be possible.
PopulateList(Data.districts, category);
}
else
{
// For each item in the list...
foreach(ListViewItem item in items)
{
// And for each sub-item in the parent item...
for(int i = 0; i < item.SubItems.Count; i++)
{
// If the lowercase value of the query is found in the lowercase value of the sub-item's text content, then its parent item is tagged as a match.
string subItemText = item.SubItems[i].Text;
int subItemLength = subItemText.Length;
// Check to see if the sub-item's text content is bigger than or equal to the query's length. This prevents an index out of range exception with the "Substring()" method.
if(subItemLength >= queryLength)
{
// To avoid using "Contains()" or "IndexOf()", I've coded my own way of checking if the sub-item's text content contains a portion of the query. Unfortunately, it only checks from the beginning of the sub-item string, so the user can't search for the "eigh" in "neighborhood" for example.
string subString = subItemText.Substring(0, queryLength).ToLower();
if(subString == query.ToLower())
{
item.Tag = "match";
}
}
}
if(item.Tag == null)
{
item.Remove();
}
}
}
}
}
private void ReadFile(string filename)
{
bool valid = true;
string exception = "";
// The districts array is reset to 0 in case the user previously chose a different data file.
Data.districts = new District[0];
StreamReader reader = new StreamReader(filename);
while(!reader.EndOfStream)
{
try
{
string districtName = reader.ReadLine();
int districtNeighborhoodCount = Convert.ToInt32(reader.ReadLine());
// Create a new "District" object.
District district = new District(districtName, districtNeighborhoodCount);
// For each neighborhood in the district...
for(int j = 0; j < districtNeighborhoodCount; j++)
{
string neighborhoodName = reader.ReadLine();
int neighborhoodPropertyCount = Convert.ToInt32(reader.ReadLine());
// Create a new "Neighborhood" object.
Neighborhood neighborhood = new Neighborhood(neighborhoodName, neighborhoodPropertyCount);
// For each property in the neighborhood...
for(int k = 0; k < neighborhoodPropertyCount; k++)
{
int propertyID = Convert.ToInt32(reader.ReadLine());
string propertyName = reader.ReadLine();
int hostID = Convert.ToInt32(reader.ReadLine());
string hostName = reader.ReadLine();
int hostPropertyCount = Convert.ToInt32(reader.ReadLine());
double latitude = Convert.ToDouble(reader.ReadLine());
double longitude = Convert.ToDouble(reader.ReadLine());
string roomType = reader.ReadLine();
double roomPrice = Convert.ToDouble(reader.ReadLine());
int roomNights = Convert.ToInt32(reader.ReadLine());
int roomAvailability = Convert.ToInt32(reader.ReadLine());
// Create a new "Property" object, and add it to the array of properties in the neighborhood.
Property property = new Property(propertyID, propertyName, hostID, hostName, hostPropertyCount, latitude, longitude, roomType, roomPrice, roomNights, roomAvailability);
neighborhood.AddProperty(property);
}
// Add the neighborhood object to the neighborhood array in the district object.
district.AddNeighborhood(neighborhood);
}
// Add each district to the main district array that'll contain all the data from the text file.
Data.AddDistrict(district);
}
catch(Exception e)
{
valid = false;
exception = e.GetType().Name;
}
}
reader.Close();
if(valid)
{
// Fill the ListViews with data.
PopulateList(Data.districts, "districts");
// Show all ListViews.
listviewDistricts.Show();
listviewNeighborhoods.Show();
listviewProperties.Show();
Utils.EnableControl(buttonAnalysis);
buttonAnalysis.Visible = true;
if(this.level == "staff")
{
buttonSave.Visible = true;
}
}
else
{
this.dataFile = "";
Data.districts = new District[0];
labelFileDialog.Text = "Select a Data Source...";
MessageBox.Show("Something went wrong. Make sure the data file is in the correct format. \n\nException Type: " + exception, "Error");
}
}
private void PopulateList(District[] districts, string category)
{
// Clear ListViews of any existing data. Coded my own method to replace the "Clear()" shortcut.
if(category == "districts")
{
listviewDistricts.Clear();
AddColumns("districts");
labelSelectDistrict.Visible = true;
labelSelectDistrictAndNeighborhood.Visible = true;
}
else if(category == "neighborhoods")
{
listviewNeighborhoods.Clear();
AddColumns("neighborhoods");
labelSelectDistrict.Visible = false;
labelSelectDistrictAndNeighborhood.Visible = true;
}
else if(category == "properties")
{
listviewProperties.Clear();
AddColumns("properties");
labelSelectDistrictAndNeighborhood.Visible = false;
}
// Populate the districts ListView.
if(category == "districts")
{
int i = 0;
foreach(District district in districts)
{
listviewDistricts.Items.Add(new ListViewItem(new string[3] { i.ToString(), district.Name, district.NeighborhoodCount.ToString() }));
i++;
}
}
// Populate the neighborhoods ListView.
else if(category == "neighborhoods" && listviewDistricts.SelectedIndices.Count == 1)
{
int districtIndex = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
int i = 0;
foreach(Neighborhood neighborhood in Data.districts[districtIndex].Neighborhoods)
{
listviewNeighborhoods.Items.Add(new ListViewItem(new string[3] { i.ToString(), neighborhood.Name, neighborhood.PropertyCount.ToString() }));
i++;
}
}
// Populate the properties ListView.
else if(category == "properties" && listviewDistricts.SelectedIndices.Count == 1 && listviewNeighborhoods.SelectedIndices.Count == 1)
{
int districtIndex = Convert.ToInt32(listviewDistricts.SelectedItems[0].SubItems[0].Text);
int neighborhoodIndex = Convert.ToInt32(listviewNeighborhoods.SelectedItems[0].SubItems[0].Text);
int i = 0;
foreach(Property property in Data.districts[districtIndex].Neighborhoods[neighborhoodIndex].Properties)
{
listviewProperties.Items.Add(new ListViewItem(new string[12] { i.ToString(), property.Id.ToString(), property.Name, property.HostID.ToString(), property.HostName, property.Count.ToString(), property.Longitude.ToString(), property.Latitude.ToString(), property.RoomType, property.RoomPrice.ToString(), property.RoomNights.ToString(), property.RoomAvailability.ToString() }));
i++;
}
}
labelFileDialog.Hide();
}
private void AddColumns(string category)
{
if(category == "districts")
{
listviewDistricts.Columns.Add("#", 0);
listviewDistricts.Columns.Add("District Name", 650);
listviewDistricts.Columns.Add("Number of Neighborhoods", 450);
}
else if(category == "neighborhoods")
{
listviewNeighborhoods.Columns.Add("#", 0);
listviewNeighborhoods.Columns.Add("Neighborhood Name", 650);
listviewNeighborhoods.Columns.Add("Number of Properties", 450);
}
else if(category == "properties")
{
listviewProperties.Columns.Add("#", 0);
listviewProperties.Columns.Add("Property ID", 130);
listviewProperties.Columns.Add("Property Name", 370);
listviewProperties.Columns.Add("Host ID", 130);
listviewProperties.Columns.Add("Host Name", 210);
listviewProperties.Columns.Add("Properties Owned", 180);
listviewProperties.Columns.Add("Longitude", 130);
listviewProperties.Columns.Add("Latitude", 130);
listviewProperties.Columns.Add("Room Type", 240);
listviewProperties.Columns.Add("Room Price", 130);
listviewProperties.Columns.Add("Minimum Nights", 150);
listviewProperties.Columns.Add("Days Available per Year", 250);
}
}
}
}