-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
224 lines (199 loc) · 9.12 KB
/
MainForm.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
using Customer_Registry.ContactFiles;
using System;
using System.Windows.Forms;
namespace Customer_Registry
{
/// <summary>
/// MainForm is the main user interface for the Customer Registry application.
/// It handles interactions such as adding, editing, and deleting customer information.
/// </summary>
public partial class MainForm : Form
{
// Manages the collection of customers.
private CustomerManager customerManager = new CustomerManager();
/// <summary>
/// Constructor for MainForm. Initializes components and sets up event handlers.
/// </summary>
public MainForm()
{
InitializeComponent();
// Event handlers for button clicks.
btnAdd.Click += BtnAdd_Click;
btnEdit.Click += BtnEdit_Click;
btnDelete.Click += BtnDelete_Click;
// Populate the customer list view on form load.
PopulateCustomerList();
listView1.SelectedIndexChanged += ListView1_SelectedIndexChanged;
listView1.MultiSelect = false;
}
/// <summary>
/// Handles the event when a customer is selected from the list.
/// Displays the selected customer's details.
/// </summary>
private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
// Extracts and displays the details of the selected customer.
ListViewItem item = listView1.SelectedItems[0];
Customer selectedCustomer = (Customer)item.Tag;
var contact = selectedCustomer.CustomerContact;
var phoneDetails = contact.PhoneDetails;
var emailDetails = contact.EmailDetails;
var addressDetails = contact.AddressDetails;
// Convert the Country enum to a string
string countryString = addressDetails.Country.ToString().Replace("_", " ");
// Formatting and displaying the contact details in the text box.
textBox1.Text = $"{contact.FirstName} {contact.LastName}\r\n" +
$" {addressDetails.Street}\r\n " +
$"{addressDetails.ZipCode} {addressDetails.City}\r\n" +
$" {countryString}\r\n" + // Use the formatted country string here
// Empty line for separation.
$" \r\n" +
$"Emails\r\n" +
$" Private {emailDetails.PersonalMail}\r\n" +
$" Office {emailDetails.OfficeMail}\r\n\n" +
// Empty line for separation.
$" \r\n" +
$"Phone numbers\r\n" +
$" Private {phoneDetails.PrivatePhone}\r\n" +
$" Office {phoneDetails.OfficePhone}";
}
else
{
// Clearing the details display if no customer is selected.
textBox1.Clear();
}
}
/// <summary>
/// Event handler for the Add button. Opens the ContactForm to add a new customer.
/// </summary>
private void BtnAdd_Click(object sender, EventArgs e)
{
ContactForm contactForm = new ContactForm();
contactForm.Text = "Add New Customer";
if (contactForm.ShowDialog() == DialogResult.OK)
{
Contact newContact = contactForm.ContactData;
Customer newCustomer = new Customer(newContact);
customerManager.Add(newCustomer);
PopulateCustomerList();
// Select the newly added customer in the ListView
SelectCustomerInListView(newCustomer.CustomerId);
}
}
/// <summary>
/// A helper method to select a customer in the ListView.
/// </summary>
/// <param name="customerId"></param>
private void SelectCustomerInListView(int customerId)
{
foreach (ListViewItem item in listView1.Items)
{
if (item.Tag is Customer customer && customer.CustomerId == customerId)
{
item.Selected = true;
listView1.EnsureVisible(item.Index);
break;
}
}
}
/// <summary>
/// Event handler for the Edit button. Opens the ContactForm to edit the selected customer's details.
/// </summary>
private void BtnEdit_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
// Store the index of the selected item
int selectedIndex = listView1.SelectedIndices[0];
ListViewItem selectedItem = listView1.SelectedItems[0];
Customer selectedCustomer = (Customer)selectedItem.Tag;
ContactForm contactForm = new ContactForm();
contactForm.Text = "Edit Customer";
if (selectedCustomer.CustomerContact != null)
{
contactForm.LoadContactData(selectedCustomer.CustomerContact);
if (contactForm.ShowDialog() == DialogResult.OK)
{
Contact updatedContact = contactForm.GetUpdatedContactData();
customerManager.Update(
selectedCustomer.CustomerId,
updatedContact.FirstName, updatedContact.LastName,
updatedContact.PhoneDetails.PrivatePhone, updatedContact.PhoneDetails.OfficePhone,
updatedContact.EmailDetails.PersonalMail, updatedContact.EmailDetails.OfficeMail,
updatedContact.AddressDetails.Street, updatedContact.AddressDetails.City,
updatedContact.AddressDetails.ZipCode, updatedContact.AddressDetails.Country);
PopulateCustomerList();
// Restore the selected index
if (selectedIndex >= 0 && selectedIndex < listView1.Items.Count)
{
listView1.Items[selectedIndex].Selected = true;
listView1.Items[selectedIndex].Focused = true;
listView1.EnsureVisible(selectedIndex); // To ensure the item is visible in the ListView
}
}
}
else
{
MessageBox.Show("Contact data is not provided for the selected customer.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Please select a customer to edit.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Event handler for the Delete button. Removes the selected customer from the registry.
/// </summary>
private void BtnDelete_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
ListViewItem selectedItem = listView1.SelectedItems[0];
Customer selectedCustomer = (Customer)selectedItem.Tag;
customerManager.Remove(selectedCustomer.CustomerId);
PopulateCustomerList();
}
else
{
MessageBox.Show("Please select a customer to delete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Populates the ListView with customer data.
/// </summary>
private void PopulateCustomerList()
{
if (customerManager == null)
{
MessageBox.Show("Customer manager is not initialized.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
listView1.Items.Clear();
var customers = customerManager.GetAllCustomers();
if (customers != null)
{
foreach (var customer in customers)
{
// Ensure the customer and its details are not null before accessing them
if (customer != null && customer.CustomerContact != null)
{
var item = new ListViewItem(new[]
{
customer.CustomerId.ToString(),
customer.CustomerContact.LastName + ", " + customer.CustomerContact.FirstName,
customer.CustomerContact.PhoneDetails?.OfficePhone,
customer.CustomerContact.EmailDetails?.OfficeMail
})
{
Tag = customer
};
listView1.Items.Add(item);
}
}
}
}
}
}