/
PopupNeighborhood.cs
226 lines (206 loc) · 6.81 KB
/
PopupNeighborhood.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SOFT152Assignment
{
public partial class PopupNeighborhood : Form
{
public string level;
public string action;
public int districtIndex;
public int neighborhoodIndex;
// For editing or viewing.
public PopupNeighborhood(string accessLevel, string desiredAction, int indexDistrict, int indexNeighborhood)
{
InitializeComponent();
this.level = accessLevel.ToLower();
this.action = desiredAction.ToLower();
this.districtIndex = indexDistrict;
this.neighborhoodIndex = indexNeighborhood;
// Autofill for changing the neighborhood name and property count.
this.inputDistrictName.Text = Data.districts[districtIndex].Name;
this.inputNeighborhoodName.Text = Data.districts[districtIndex].Neighborhoods[neighborhoodIndex].Name;
this.inputPropertyCount.Text = Data.districts[districtIndex].Neighborhoods[neighborhoodIndex].PropertyCount.ToString();
if(action == "edit")
{
// This used to allow users to delete a neighborhood, but I realized it'd count as adding to the functionality of the program, and it'd fall outside the brief.
// this.buttonDelete.Visible = true;
}
// If the user only wants to view the item, then the TextBox components are set to read-only.
else if(action == "view")
{
this.inputNeighborhoodName.ReadOnly = true;
this.inputNeighborhoodName.Enabled = false;
this.inputPropertyCount.ReadOnly = true;
this.inputPropertyCount.Enabled = false;
this.panelCount.Show();
this.buttonConfirm.Hide();
}
}
// For adding, but with the district name autofilled.
public PopupNeighborhood(string accessLevel, string desiredAction, int indexDistrict)
{
InitializeComponent();
this.level = accessLevel.ToLower();
this.action = desiredAction.ToLower();
this.districtIndex = indexDistrict;
// Autofill for changing the neighborhood name and property count.
this.inputDistrictName.Text = Data.districts[districtIndex].Name;
// If the user only wants to view the item, then the TextBox components are set to read-only.
if(action == "view")
{
this.inputNeighborhoodName.ReadOnly = true;
this.inputNeighborhoodName.Enabled = false;
this.inputPropertyCount.ReadOnly = true;
this.inputPropertyCount.Enabled = false;
this.panelCount.Show();
this.buttonConfirm.Hide();
}
}
private void SetTitle()
{
// When a "popup" form is opened, the action the user wants to perform is passed to the popup form's class, and is used to determine what the window title of the form should be.
if(action == "edit")
{
this.Text = "Edit Neighborhood";
}
else if(action == "add")
{
this.Text = "Add Neighborhood";
}
else if(action == "view")
{
this.Text = "View Neighborhood";
}
}
private void PanelName_Click(object sender, EventArgs e)
{
// User might click on the panel instead of the TextBox. This takes care of that.
inputNeighborhoodName.Focus();
}
private void PanelCount_Click(object sender, EventArgs e)
{
// User might click on the panel instead of the TextBox. This takes care of that.
inputPropertyCount.Focus();
}
private void ButtonNext_Click(object sender, EventArgs e)
{
ProcessInput();
}
private void InputNeighborhoodName_Enter(object sender, EventArgs e)
{
// Empty "placeholder".
if(inputNeighborhoodName.Text == "Neighborhood Name...")
{
inputNeighborhoodName.ForeColor = Color.FromArgb(250, 250, 250);
inputNeighborhoodName.Text = "";
}
}
private void InputPropertyCount_Enter(object sender, EventArgs e)
{
// Empty "placeholder".
if(inputPropertyCount.Text == "Number of Properties...")
{
inputPropertyCount.ForeColor = Color.FromArgb(250, 250, 250);
inputPropertyCount.Text = "";
}
}
private void InputDistrictName_Enter(object sender, EventArgs e)
{
// Empty "placeholder".
if(inputDistrictName.Text == "District Name...")
{
inputDistrictName.ForeColor = Color.FromArgb(250, 250, 250);
inputDistrictName.Text = "";
}
}
private void InputNeighborhoodName_Leave(object sender, EventArgs e)
{
// Fill "placeholder".
if(inputNeighborhoodName.Text.Trim() == "")
{
inputNeighborhoodName.Text = "Neighborhood Name...";
}
}
private void InputPropertyCount_Leave(object sender, EventArgs e)
{
// Fill "placeholder".
if(inputPropertyCount.Text.Trim() == "")
{
inputPropertyCount.Text = "Number of Properties...";
}
}
private void InputDistrictName_Leave(object sender, EventArgs e)
{
// Fill "placeholder".
if(inputDistrictName.Text == "")
{
inputDistrictName.Text = "District Name...";
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
District district = Data.districts[districtIndex];
district.Neighborhoods[neighborhoodIndex] = null;
Neighborhood[] neighborhoods = new Neighborhood[0];
foreach(Neighborhood neighborhood in district.Neighborhoods)
{
if(neighborhood != null)
{
int numberOfNeighborhoods = neighborhoods.Length;
Array.Resize(ref neighborhoods, numberOfNeighborhoods + 1);
neighborhoods[numberOfNeighborhoods] = neighborhood;
}
}
Data.districts[districtIndex].NeighborhoodCount -= 1;
Data.districts[districtIndex].Neighborhoods = neighborhoods;
Data.changed = true;
Data.deletedNeighborhood = true;
this.Hide();
}
private void InputNeighborhoodName_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
ProcessInput();
}
}
private void ProcessInput()
{
// The boolean variable "valid" is used to determine whether or not the TextBoxes have been filled out. If they have, then the next button does what's it's actually meant to do.
bool valid = true;
if(inputNeighborhoodName.Text.Trim() == "" || inputNeighborhoodName.Text == "Neighborhood Name...")
{
inputNeighborhoodName.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(valid)
{
if(this.action == "add")
{
District district = Data.districts[districtIndex];
Neighborhood neighborhood = new Neighborhood(inputNeighborhoodName.Text, 0);
int numberOfNeighborhoods = district.Neighborhoods.Length;
district.AddNeighborhood(neighborhood);
Data.districts[districtIndex] = district;
}
else if(this.action == "edit")
{
District district = Data.districts[districtIndex];
Neighborhood neighborhood = district.Neighborhoods[neighborhoodIndex];
neighborhood.Name = inputNeighborhoodName.Text;
Data.districts[districtIndex].Neighborhoods[neighborhoodIndex].Name = inputNeighborhoodName.Text;
}
Data.changed = true;
this.Hide();
}
}
}
}