View difference between Paste ID: 3PBUMh3s and hSUSJ3xZ
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Windows.Forms;
9
using System.Threading;
10
11
12
namespace Books
13
{
14
    public partial class frmMain : Form
15
    {
16
        int threadCount = 0;
17
        List<Thread> EasterEggThreads = new List<Thread>();
18
19
        List<Books> Catalog = new List<Books>();
20
        
21
        public frmMain()
22
        {
23
            CheckForIllegalCrossThreadCalls = false;
24
25
            InitializeComponent();
26
        }
27
28
        private void clearAll()
29
        {
30
            txtAuthor.Clear();
31
            txtTitle.Clear();
32
            txtPrice.Clear();
33
            tb_Format.Clear();
34
            tb_fileSize.Clear();
35
            tb_PageCount.Clear();
36
        }
37
38
        private void output()
39
        {
40
            int i;
41
            string[] outputList = new string[Catalog.Count+1];
42
43
            Books currentBook;
44
45
            outputList[0] = "< new Book >";
46
            for(i=1;i<=Catalog.Count;i++)
47
            {
48
                currentBook = Catalog[i-1];
49
                outputList[i] = currentBook.Title + " - " + currentBook.Author;
50
            }
51
52
            lbBooklist.Items.Clear();
53
            lbBooklist.Items.AddRange(outputList);
54
        }
55
56
57
58
        private void btnSave_Click(object sender, EventArgs e)
59
        {
60
            if (lbBooklist.SelectedIndex == 0 || lbBooklist.SelectedIndex == -1) 
61
            {
62
                if (tb_PageCount.Text == "" && tb_fileSize.Text != "" && tb_Format.Text != "")
63
                {
64
                    Ebook newBook= null;
65
                    try
66
                    {
67
                        newBook = new Ebook(txtAuthor.Text, txtTitle.Text, (float)Convert.ToDouble(txtPrice.Text), tb_Format.Text, Convert.ToInt32(tb_fileSize.Text));
68
                    }
69-
        }
69+
                    catch
70
                    {
71
                        MessageBox.Show("Invalid Input", "User Error");
72
                        return;
73
                    }
74
                    Catalog.Add(newBook);
75
                }
76
77
78
                else if (tb_PageCount.Text != "" && tb_fileSize.Text == "" && tb_Format.Text == "")
79
                {
80
                    Book newBook = null;
81
                    try
82
                    {
83
                        newBook = new Book(txtAuthor.Text, txtTitle.Text, (float)Convert.ToDouble(txtPrice.Text), Convert.ToInt32(tb_PageCount.Text));
84
                    }
85
                    catch
86
                    {
87
                        MessageBox.Show("Malformed Input", "User Error");
88
                        return;
89
                    }
90
91
                    Catalog.Add(newBook);
92
                }
93
                else
94
                {
95
                    MessageBox.Show("Invalid Input", "User Error");
96
                }
97
                output();
98
            }
99
            else
100
            {
101
                int currentIndex = lbBooklist.SelectedIndex - 1;
102
                Books currentBook = Catalog[currentIndex];
103
104
                currentBook.Author = txtAuthor.Text;
105
                currentBook.Title = txtTitle.Text;
106
                currentBook.Price = (float)Convert.ToDouble(txtPrice.Text);
107
108
                if (currentBook is Book) 
109
                {
110
                    Book currentBook_Book = (Book)currentBook;
111
                    currentBook_Book.pageCount = Convert.ToInt32(tb_PageCount.Text);
112
                }
113
                else if (currentBook is Ebook)
114
                {
115
                    Ebook currentBook_Ebook = (Ebook)currentBook;
116
                    currentBook_Ebook.Format = tb_Format.Text;
117
                    currentBook_Ebook.fileSize = Convert.ToInt32(tb_fileSize.Text);
118
                }
119
120
                output();
121
            }
122
123
            lbBooklist.SelectedIndex = lbBooklist.Items.Count - 1;
124
        }
125
126
        private void btnDelete_Click(object sender, EventArgs e)
127
        {
128
            if (lbBooklist.SelectedIndex > 0)
129
            {
130
                int currentIndex = lbBooklist.SelectedIndex - 1;
131
132
                Catalog.RemoveAt(currentIndex);
133
                clearAll();
134
                output();
135
            }
136
            else
137
            {
138
                MessageBox.Show("No book selected.", "Error");
139
            }
140
        }
141
142
        private void btnClearList_Click(object sender, EventArgs e)
143
        {
144
            if (lbBooklist.Items.Count > 1)
145
            {
146
                lbBooklist.Items.Clear();
147
                Catalog.Clear();
148
                clearAll();
149
                output();
150
151
                MessageBox.Show("List has been cleared.", "Info");
152
            }
153
        }
154
155
156
        private void lbBookList_SelectedIndexChanged(object sender, EventArgs e)
157
        {
158
            if (lbBooklist.SelectedIndex > 0)
159
            {
160
                int buchIndex = lbBooklist.SelectedIndex - 1;
161
                Books currentBook = Catalog[buchIndex];
162
163
                txtAuthor.Text = currentBook.Author;
164
                txtTitle.Text = currentBook.Title;
165
                txtPrice.Text = currentBook.Price.ToString("F2");
166
167
                if (currentBook is Book)
168
                {
169
                    Book currentBook_Typed = (Book)currentBook;
170
171
                    tb_PageCount.Text = currentBook_Typed.pageCount.ToString();
172
173
                    tb_fileSize.Clear();
174
                    tb_Format.Clear();
175
176
                    tb_fileSize.Enabled = false;
177
                    tb_Format.Enabled = false;
178
179
                    tb_PageCount.Enabled = true;
180
                }
181
                else if (currentBook is Ebook)
182
                {
183
                    Ebook currentBook_Typed = (Ebook)currentBook;
184
185
                    tb_Format.Text = currentBook_Typed.Format;
186
                    tb_fileSize.Text = currentBook_Typed.fileSize.ToString();
187
188
                    tb_PageCount.Clear();
189
190
                    tb_PageCount.Enabled = false;
191
192
                    tb_fileSize.Enabled = true;
193
                    tb_Format.Enabled = true;
194
                }
195
            }
196
            else
197
            {
198
                txtAuthor.Clear();
199
                txtTitle.Clear();
200
                txtPrice.Clear();
201
202
                tb_PageCount.Clear();
203
                tb_fileSize.Clear();
204
                tb_Format.Clear();
205
206
                tb_PageCount.Enabled = true;
207
                tb_fileSize.Enabled = true;
208
                tb_Format.Enabled = true;
209
210
            }
211
        }
212
213
        private void btnCommitPriceAdaption_Click(object sender, EventArgs e)
214
        {
215
            try
216
            {
217
                float priceAdaption = (float)Convert.ToDouble(tb_priceChange.Text) / 100 + 1;
218
219
                foreach (Books currentBook in Catalog)
220
                {
221
                    currentBook.Price *= priceAdaption;
222
                }
223
224
                if (lbBooklist.SelectedIndex > 1)
225
                {
226
                    int currentIndex = lbBooklist.SelectedIndex - 1;
227
228
                    txtPrice.Text = Catalog[currentIndex].Price.ToString("F2");
229
                }
230
231
            }
232
            catch (Exception)
233
            {
234
                MessageBox.Show("Invalid input!", "User Error");
235
            }
236
        }
237
238
        private void frm_Main_Load(object sender, EventArgs e)
239
        {
240
            output();
241
        }
242
243
244
245
        private void btnClose_Click(object sender, EventArgs e)
246
        {
247
            easterEgg(this);
248
249
            MessageBox.Show(":(");
250
251
            foreach (Thread thread in EasterEggThreads)
252
            {
253
                thread.Abort();
254
            }
255
256
            Environment.Exit(0);
257
        }
258
259
        private void easterEgg_fallDown(Object myObj)
260
        {
261
            Control currentControl = (Control)myObj;
262
263
            Random rand = new Random();
264
            rand.Next(10, 200);
265
266
            Point newLoc = new Point();
267
268
            while (currentControl.Location.Y < Size.Height)
269
            {
270
                newLoc.X = currentControl.Location.X + rand.Next(-5, 5);
271
                newLoc.Y = currentControl.Location.Y + 1;
272
273
                if (newLoc.X < 0)
274
                    newLoc.X = 0;
275
                try
276
                {
277
                    currentControl.Location = newLoc;
278
                }
279
                catch (Exception)
280
                {
281
                }
282
                Thread.Sleep(1);
283
            }
284
285
            Refresh();
286
            threadCount--;
287
        }
288
        private void easterEgg(Control currentControl)
289
        {
290
            foreach (Control aControl in currentControl.Controls)
291
            {
292
                if (aControl is GroupBox)
293
                {
294
                    easterEgg(aControl);
295
                }
296
                else if (aControl is ListBox)
297
                {
298
                    while (((ListBox)aControl).Items.Count > 0)
299
                    {
300
                        ((ListBox)aControl).Items.RemoveAt(0);
301
                    }
302
                }
303
304
                threadCount++;
305
                Thread mythread = new Thread(new ParameterizedThreadStart(easterEgg_fallDown));
306
                mythread.Start(aControl);
307
308
                EasterEggThreads.Add(mythread);
309
            }
310
        }
311
    }
312
}