Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace Books
- {
- public partial class frmMain : Form
- {
- int threadCount = 0;
- List<Thread> EasterEggThreads = new List<Thread>();
- List<Books> Catalog = new List<Books>();
- public frmMain()
- {
- CheckForIllegalCrossThreadCalls = false;
- InitializeComponent();
- }
- private void clearAll()
- {
- txtAuthor.Clear();
- txtTitle.Clear();
- txtPrice.Clear();
- tb_Format.Clear();
- tb_fileSize.Clear();
- tb_PageCount.Clear();
- }
- private void output()
- {
- int i;
- string[] outputList = new string[Catalog.Count+1];
- Books currentBook;
- outputList[0] = "< new Book >";
- for(i=1;i<=Catalog.Count;i++)
- {
- currentBook = Catalog[i-1];
- outputList[i] = currentBook.Title + " - " + currentBook.Author;
- }
- lbBooklist.Items.Clear();
- lbBooklist.Items.AddRange(outputList);
- }
- private void btnSave_Click(object sender, EventArgs e)
- {
- if (lbBooklist.SelectedIndex == 0 || lbBooklist.SelectedIndex == -1)
- {
- if (tb_PageCount.Text == "" && tb_fileSize.Text != "" && tb_Format.Text != "")
- {
- Ebook newBook= null;
- try
- {
- newBook = new Ebook(txtAuthor.Text, txtTitle.Text, (float)Convert.ToDouble(txtPrice.Text), tb_Format.Text, Convert.ToInt32(tb_fileSize.Text));
- }
- catch
- {
- MessageBox.Show("Invalid Input", "User Error");
- return;
- }
- Catalog.Add(newBook);
- }
- else if (tb_PageCount.Text != "" && tb_fileSize.Text == "" && tb_Format.Text == "")
- {
- Book newBook = null;
- try
- {
- newBook = new Book(txtAuthor.Text, txtTitle.Text, (float)Convert.ToDouble(txtPrice.Text), Convert.ToInt32(tb_PageCount.Text));
- }
- catch
- {
- MessageBox.Show("Malformed Input", "User Error");
- return;
- }
- Catalog.Add(newBook);
- }
- else
- {
- MessageBox.Show("Invalid Input", "User Error");
- }
- output();
- }
- else
- {
- int currentIndex = lbBooklist.SelectedIndex - 1;
- Books currentBook = Catalog[currentIndex];
- currentBook.Author = txtAuthor.Text;
- currentBook.Title = txtTitle.Text;
- currentBook.Price = (float)Convert.ToDouble(txtPrice.Text);
- if (currentBook is Book)
- {
- Book currentBook_Book = (Book)currentBook;
- currentBook_Book.pageCount = Convert.ToInt32(tb_PageCount.Text);
- }
- else if (currentBook is Ebook)
- {
- Ebook currentBook_Ebook = (Ebook)currentBook;
- currentBook_Ebook.Format = tb_Format.Text;
- currentBook_Ebook.fileSize = Convert.ToInt32(tb_fileSize.Text);
- }
- output();
- }
- lbBooklist.SelectedIndex = lbBooklist.Items.Count - 1;
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- if (lbBooklist.SelectedIndex > 0)
- {
- int currentIndex = lbBooklist.SelectedIndex - 1;
- Catalog.RemoveAt(currentIndex);
- clearAll();
- output();
- }
- else
- {
- MessageBox.Show("No book selected.", "Error");
- }
- }
- private void btnClearList_Click(object sender, EventArgs e)
- {
- if (lbBooklist.Items.Count > 1)
- {
- lbBooklist.Items.Clear();
- Catalog.Clear();
- clearAll();
- output();
- MessageBox.Show("List has been cleared.", "Info");
- }
- }
- private void lbBookList_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (lbBooklist.SelectedIndex > 0)
- {
- int buchIndex = lbBooklist.SelectedIndex - 1;
- Books currentBook = Catalog[buchIndex];
- txtAuthor.Text = currentBook.Author;
- txtTitle.Text = currentBook.Title;
- txtPrice.Text = currentBook.Price.ToString("F2");
- if (currentBook is Book)
- {
- Book currentBook_Typed = (Book)currentBook;
- tb_PageCount.Text = currentBook_Typed.pageCount.ToString();
- tb_fileSize.Clear();
- tb_Format.Clear();
- tb_fileSize.Enabled = false;
- tb_Format.Enabled = false;
- tb_PageCount.Enabled = true;
- }
- else if (currentBook is Ebook)
- {
- Ebook currentBook_Typed = (Ebook)currentBook;
- tb_Format.Text = currentBook_Typed.Format;
- tb_fileSize.Text = currentBook_Typed.fileSize.ToString();
- tb_PageCount.Clear();
- tb_PageCount.Enabled = false;
- tb_fileSize.Enabled = true;
- tb_Format.Enabled = true;
- }
- }
- else
- {
- txtAuthor.Clear();
- txtTitle.Clear();
- txtPrice.Clear();
- tb_PageCount.Clear();
- tb_fileSize.Clear();
- tb_Format.Clear();
- tb_PageCount.Enabled = true;
- tb_fileSize.Enabled = true;
- tb_Format.Enabled = true;
- }
- }
- private void btnCommitPriceAdaption_Click(object sender, EventArgs e)
- {
- try
- {
- float priceAdaption = (float)Convert.ToDouble(tb_priceChange.Text) / 100 + 1;
- foreach (Books currentBook in Catalog)
- {
- currentBook.Price *= priceAdaption;
- }
- if (lbBooklist.SelectedIndex > 1)
- {
- int currentIndex = lbBooklist.SelectedIndex - 1;
- txtPrice.Text = Catalog[currentIndex].Price.ToString("F2");
- }
- }
- catch (Exception)
- {
- MessageBox.Show("Invalid input!", "User Error");
- }
- }
- private void frm_Main_Load(object sender, EventArgs e)
- {
- output();
- }
- private void btnClose_Click(object sender, EventArgs e)
- {
- easterEgg(this);
- MessageBox.Show(":(");
- foreach (Thread thread in EasterEggThreads)
- {
- thread.Abort();
- }
- Environment.Exit(0);
- }
- private void easterEgg_fallDown(Object myObj)
- {
- Control currentControl = (Control)myObj;
- Random rand = new Random();
- rand.Next(10, 200);
- Point newLoc = new Point();
- while (currentControl.Location.Y < Size.Height)
- {
- newLoc.X = currentControl.Location.X + rand.Next(-5, 5);
- newLoc.Y = currentControl.Location.Y + 1;
- if (newLoc.X < 0)
- newLoc.X = 0;
- try
- {
- currentControl.Location = newLoc;
- }
- catch (Exception)
- {
- }
- Thread.Sleep(1);
- }
- Refresh();
- threadCount--;
- }
- private void easterEgg(Control currentControl)
- {
- foreach (Control aControl in currentControl.Controls)
- {
- if (aControl is GroupBox)
- {
- easterEgg(aControl);
- }
- else if (aControl is ListBox)
- {
- while (((ListBox)aControl).Items.Count > 0)
- {
- ((ListBox)aControl).Items.RemoveAt(0);
- }
- }
- threadCount++;
- Thread mythread = new Thread(new ParameterizedThreadStart(easterEgg_fallDown));
- mythread.Start(aControl);
- EasterEggThreads.Add(mythread);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement