-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTripCreate.cs
More file actions
251 lines (220 loc) · 10.6 KB
/
Copy pathTripCreate.cs
File metadata and controls
251 lines (220 loc) · 10.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
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
namespace H1
{
public partial class TripCreate : Form
{
// Colors based on the provided specs
private Color navbarColor = Color.FromArgb(44, 62, 80);
private Color headerColor = Color.FromArgb(52, 152, 219);
private Color whiteColor = Color.White;
private Color lightGrayColor = Color.FromArgb(240, 240, 240);
// Data storage
private DataTable tripsData = new DataTable();
private DataTable categoriesData = new DataTable();
public TripCreate()
{
InitializeComponent();
InitializeData();
LoadSampleData();
}
private void InitializeData()
{
// Setup trips data table
tripsData.Columns.Add("TripID", typeof(int));
tripsData.Columns.Add("Title", typeof(string));
tripsData.Columns.Add("Description", typeof(string));
tripsData.Columns.Add("Destination", typeof(string));
tripsData.Columns.Add("CategoryID", typeof(int));
tripsData.Columns.Add("Category", typeof(string));
tripsData.Columns.Add("StartDate", typeof(DateTime));
tripsData.Columns.Add("EndDate", typeof(DateTime));
tripsData.Columns.Add("Price", typeof(decimal));
tripsData.Columns.Add("MaxCapacity", typeof(int));
tripsData.Columns.Add("Difficulty", typeof(string));
tripsData.Columns.Add("Status", typeof(string));
// Setup categories data table
categoriesData.Columns.Add("CategoryID", typeof(int));
categoriesData.Columns.Add("Name", typeof(string));
categoriesData.Columns.Add("Description", typeof(string));
}
private void LoadSampleData()
{
// Load sample categories
categoriesData.Rows.Add(1, "Adventure", "High-energy outdoor activities");
categoriesData.Rows.Add(2, "Cultural", "Immersive cultural experiences");
categoriesData.Rows.Add(3, "Beach", "Relaxing beach vacations");
categoriesData.Rows.Add(4, "Mountain", "Mountain hiking and climbing");
categoriesData.Rows.Add(5, "City Tour", "Urban exploration tours");
categoriesData.Rows.Add(6, "Wildlife", "Wildlife safaris and encounters");
categoriesData.Rows.Add(7, "Historical", "Historical sites and museums");
// Load sample trips
tripsData.Rows.Add(1, "Paris City Tour", "Explore the beautiful city of Paris", "Paris", 5, "City Tour",
new DateTime(2025, 5, 15), new DateTime(2025, 5, 20), 1200m, 25, "Easy", "Active");
tripsData.Rows.Add(2, "Alpine Adventure", "Challenging mountain hikes", "Swiss Alps", 4, "Mountain",
new DateTime(2025, 6, 10), new DateTime(2025, 6, 17), 1800m, 15, "Hard", "Active");
tripsData.Rows.Add(3, "Safari Experience", "African wildlife safari", "Serengeti", 6, "Wildlife",
new DateTime(2025, 7, 5), new DateTime(2025, 7, 12), 2500m, 12, "Medium", "Active");
// Bind data to controls
dgvTrips.DataSource = tripsData;
cmbCategory.DataSource = categoriesData;
cmbCategory.DisplayMember = "Name";
cmbCategory.ValueMember = "CategoryID";
FormatTripsGrid();
}
private void FormatTripsGrid()
{
dgvTrips.Columns["TripID"].Visible = false;
dgvTrips.Columns["CategoryID"].Visible = false;
dgvTrips.Columns["Description"].Visible = false;
dgvTrips.Columns["Title"].HeaderText = "Trip Title";
dgvTrips.Columns["Destination"].HeaderText = "Destination";
dgvTrips.Columns["StartDate"].HeaderText = "Start Date";
dgvTrips.Columns["EndDate"].HeaderText = "End Date";
dgvTrips.Columns["Price"].HeaderText = "Price (USD)";
dgvTrips.Columns["MaxCapacity"].HeaderText = "Capacity";
dgvTrips.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgvTrips.RowHeadersVisible = false;
dgvTrips.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
private void btnCreateTrip_Click(object sender, EventArgs e)
{
if (ValidateTripForm())
{
// Create new trip
DataRow newTrip = tripsData.NewRow();
newTrip["TripID"] = tripsData.Rows.Count + 1;
newTrip["Title"] = txtTitle.Text;
newTrip["Description"] = txtDescription.Text;
newTrip["Destination"] = txtDestination.Text;
newTrip["CategoryID"] = cmbCategory.SelectedValue;
newTrip["Category"] = cmbCategory.Text;
newTrip["StartDate"] = dtpStartDate.Value;
newTrip["EndDate"] = dtpEndDate.Value;
newTrip["Price"] = decimal.Parse(txtPrice.Text);
newTrip["MaxCapacity"] = int.Parse(txtCapacity.Text);
newTrip["Difficulty"] = cmbDifficulty.SelectedItem.ToString();
newTrip["Status"] = "Active";
tripsData.Rows.Add(newTrip);
ClearForm();
MessageBox.Show("Trip created successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private bool ValidateTripForm()
{
if (string.IsNullOrWhiteSpace(txtTitle.Text))
{
MessageBox.Show("Please enter a trip title", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
if (string.IsNullOrWhiteSpace(txtDestination.Text))
{
MessageBox.Show("Please enter a destination", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
if (dtpStartDate.Value >= dtpEndDate.Value)
{
MessageBox.Show("End date must be after start date", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
if (!decimal.TryParse(txtPrice.Text, out decimal price) || price <= 0)
{
MessageBox.Show("Please enter a valid price", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
if (!int.TryParse(txtCapacity.Text, out int capacity) || capacity <= 0)
{
MessageBox.Show("Please enter a valid capacity", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
private void ClearForm()
{
txtTitle.Clear();
txtDescription.Clear();
txtDestination.Clear();
cmbCategory.SelectedIndex = 0;
dtpStartDate.Value = DateTime.Now;
dtpEndDate.Value = DateTime.Now.AddDays(1);
txtPrice.Clear();
txtCapacity.Clear();
cmbDifficulty.SelectedIndex = 0;
}
private void dgvTrips_SelectionChanged(object sender, EventArgs e)
{
if (dgvTrips.SelectedRows.Count > 0)
{
DataRowView row = (DataRowView)dgvTrips.SelectedRows[0].DataBoundItem;
LoadTripDetails(row.Row);
}
}
private void LoadTripDetails(DataRow trip)
{
txtTitle.Text = trip["Title"].ToString();
txtDescription.Text = trip["Description"].ToString();
txtDestination.Text = trip["Destination"].ToString();
cmbCategory.SelectedValue = trip["CategoryID"];
dtpStartDate.Value = (DateTime)trip["StartDate"];
dtpEndDate.Value = (DateTime)trip["EndDate"];
txtPrice.Text = trip["Price"].ToString();
txtCapacity.Text = trip["MaxCapacity"].ToString();
cmbDifficulty.SelectedItem = trip["Difficulty"].ToString();
btnUpdateTrip.Enabled = true;
btnDeleteTrip.Enabled = true;
btnCreateTrip.Enabled = false;
}
private void btnUpdateTrip_Click(object sender, EventArgs e)
{
if (dgvTrips.SelectedRows.Count == 0)
{
MessageBox.Show("Please select a trip to update", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (ValidateTripForm())
{
DataRowView row = (DataRowView)dgvTrips.SelectedRows[0].DataBoundItem;
row["Title"] = txtTitle.Text;
row["Description"] = txtDescription.Text;
row["Destination"] = txtDestination.Text;
row["CategoryID"] = cmbCategory.SelectedValue;
row["Category"] = cmbCategory.Text;
row["StartDate"] = dtpStartDate.Value;
row["EndDate"] = dtpEndDate.Value;
row["Price"] = decimal.Parse(txtPrice.Text);
row["MaxCapacity"] = int.Parse(txtCapacity.Text);
row["Difficulty"] = cmbDifficulty.SelectedItem.ToString();
MessageBox.Show("Trip updated successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
ClearForm();
btnCreateTrip.Enabled = true;
}
}
private void btnDeleteTrip_Click(object sender, EventArgs e)
{
if (dgvTrips.SelectedRows.Count == 0)
{
MessageBox.Show("Please select a trip to delete", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var confirm = MessageBox.Show("Are you sure you want to delete this trip?", "Confirm Delete",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (confirm == DialogResult.Yes)
{
DataRowView row = (DataRowView)dgvTrips.SelectedRows[0].DataBoundItem;
tripsData.Rows.Remove(row.Row);
ClearForm();
btnCreateTrip.Enabled = true;
MessageBox.Show("Trip deleted successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnClearForm_Click(object sender, EventArgs e)
{
ClearForm();
btnCreateTrip.Enabled = true;
btnUpdateTrip.Enabled = false;
btnDeleteTrip.Enabled = false;
}
}
}