-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomPrizeForm.cs
More file actions
316 lines (258 loc) · 11.2 KB
/
RandomPrizeForm.cs
File metadata and controls
316 lines (258 loc) · 11.2 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
namespace RandomPrizeDrawer
{
public partial class RandomPrizeForm : Form
{
private const string SessionFilePath = "session.json";
public RandomPrizeForm()
{
InitializeComponent();
listBoxParticipants.AllowDrop = true;
listBoxPrizes.AllowDrop = true;
listBoxParticipants.DragEnter += ListBoxParticipants_DragEnter;
listBoxParticipants.DragDrop += ListBoxParticipants_DragDrop;
listBoxPrizes.DragEnter += ListBoxPrizes_DragEnter;
listBoxPrizes.DragDrop += ListBoxPrizes_DragDrop;
}
private void buttonAddParticipant_Click(object sender, EventArgs e)
{
string participant = textBoxParticipant.Text.Trim();
if (string.IsNullOrEmpty(participant))
{
MessageBox.Show("Please enter a participant name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (listBoxParticipants.Items.Contains(participant))
{
MessageBox.Show("This participant is already in the list.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
listBoxParticipants.Items.Add(participant);
textBoxParticipant.Clear();
}
private void buttonAddPrize_Click(object sender, EventArgs e)
{
string prize = textBoxPrize.Text.Trim();
if (string.IsNullOrEmpty(prize))
{
MessageBox.Show("Please enter a prize name.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (listBoxPrizes.Items.Contains(prize))
{
MessageBox.Show("This prize is already in the list.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
listBoxPrizes.Items.Add(prize);
textBoxPrize.Clear();
}
private void buttonDrawWinner_Click(object sender, EventArgs e)
{
if (listBoxParticipants.Items.Count == 0 || listBoxPrizes.Items.Count == 0)
{
MessageBox.Show("Please make sure both participants and prizes are available before drawing.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
Random random = new Random();
int participantIndex = random.Next(listBoxParticipants.Items.Count);
int prizeIndex = random.Next(listBoxPrizes.Items.Count);
string selectedParticipant = listBoxParticipants.Items[participantIndex].ToString();
string selectedPrize = listBoxPrizes.Items[prizeIndex].ToString();
listBoxWinners.Items.Add($"{selectedParticipant} wins {selectedPrize}!");
listBoxParticipants.Items.RemoveAt(participantIndex);
listBoxPrizes.Items.RemoveAt(prizeIndex);
}
private void buttonReset_Click(object sender, EventArgs e)
{
listBoxParticipants.Items.Clear();
listBoxPrizes.Items.Clear();
listBoxWinners.Items.Clear();
textBoxParticipant.Clear();
textBoxPrize.Clear();
}
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
var session = new
{
Participants = listBoxParticipants.Items.Cast<string>().ToList(),
Prizes = listBoxPrizes.Items.Cast<string>().ToList(),
Winners = listBoxWinners.Items.Cast<string>().ToList()
};
string json = System.Text.Json.JsonSerializer.Serialize(session, new System.Text.Json.JsonSerializerOptions
{
WriteIndented = true
});
File.WriteAllText("session.json", json);
MessageBox.Show("Session saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to save session: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonLoad_Click(object sender, EventArgs e)
{
try
{
if (!File.Exists(SessionFilePath))
{
MessageBox.Show("No saved session found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string json = File.ReadAllText(SessionFilePath);
var sessionData = System.Text.Json.JsonSerializer.Deserialize<SessionData>(json);
if (sessionData != null)
{
listBoxParticipants.Items.Clear();
listBoxPrizes.Items.Clear();
listBoxWinners.Items.Clear();
foreach (var participant in sessionData.Participants ?? new List<string>())
{
listBoxParticipants.Items.Add(participant);
}
foreach (var prize in sessionData.Prizes ?? new List<string>())
{
listBoxPrizes.Items.Add(prize);
}
foreach (var winner in sessionData.Winners ?? new List<string>())
{
listBoxWinners.Items.Add(winner);
}
MessageBox.Show("Session loaded successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Failed to parse session data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"Failed to load session: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private class SessionData
{
public List<string>? Participants { get; set; }
public List<string>? Winners { get; set; }
public List<string>? Prizes { get; set; }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (var brush = new System.Drawing.Drawing2D.LinearGradientBrush(
this.ClientRectangle,
Color.LightSkyBlue, // Top color
Color.Orange, // Bottom color
System.Drawing.Drawing2D.LinearGradientMode.Vertical))
{
e.Graphics.FillRectangle(brush, this.ClientRectangle);
}
using (Pen borderPen = new Pen(Color.DarkSlateGray, 2))
{
e.Graphics.DrawRectangle(borderPen,
listBoxParticipants.Left - 5, listBoxParticipants.Top - 5,
listBoxParticipants.Width + 10, listBoxParticipants.Height + 10);
e.Graphics.DrawRectangle(borderPen,
listBoxWinners.Left - 5, listBoxWinners.Top - 5,
listBoxWinners.Width + 10, listBoxWinners.Height + 10);
e.Graphics.DrawRectangle(borderPen,
listBoxPrizes.Left - 5, listBoxPrizes.Top - 5,
listBoxPrizes.Width + 10, listBoxPrizes.Height + 10);
}
}
private void ListBoxPrizes_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.All(file => file.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void ListBoxPrizes_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
if (file.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
try
{
string[] lines = File.ReadAllLines(file);
foreach (string line in lines)
{
if (!string.IsNullOrWhiteSpace(line))
{
listBoxPrizes.Items.Add(line.Trim());
}
}
MessageBox.Show($"Prizes from {Path.GetFileName(file)} have been added!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Error reading file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void ListBoxParticipants_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
// Only allow text files
if (files.All(file => file.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void ListBoxParticipants_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
if (file.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
try
{
// Read all lines from the file
string[] lines = File.ReadAllLines(file);
foreach (string line in lines)
{
// Add each line as a participant if it's not empty
if (!string.IsNullOrWhiteSpace(line))
{
listBoxParticipants.Items.Add(line.Trim());
}
}
MessageBox.Show($"Participants from {Path.GetFileName(file)} have been added!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Error reading file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
}