private Library lib; // The library #endregion Fields #region Constructors // Precondition: None // Postcondition: The form's GUI is prepared for display. A few test items and patrons // are added to the library public Prog2Form() { InitializeComponent(); lib = new Library(); // Create the library // Insert test data - Magic numbers allowed here lib.AddLibraryBook("The Wright Guide to C#", "UofL Press", 2010, 14, "ZZ225 3G", "Andrew Wright"); lib.AddLibraryBook("Harriet Pooter", "Stealer Books", 2000, 21, "AG773 ZF", "IP Thief"); lib.AddLibraryMovie("Andrew's Super-Duper Movie", "UofL Movies", 2011, 7, "MM33 2D", 92.5, "Andrew L. Wright", LibraryMediaItem.MediaType.BLURAY, LibraryMovie.MPAARatings.PG); lib.AddLibraryMovie("Pirates of the Carribean: The Curse of C#", "Disney Programming", 2011, 10, "MO93 4S", 122.5, "Steven Stealberg", LibraryMediaItem.MediaType.DVD, LibraryMovie.MPAARatings.G); lib.AddLibraryMusic("C# - The Album", "UofL Music", 2011, 14, "CD44 4Z", 84.3, "Dr. A", LibraryMediaItem.MediaType.CD, 10); lib.AddLibraryMusic("The Sounds of Programming", "Soundproof Music", 1996, 21, "VI64 1Z", 65.0, "Cee Sharpe", LibraryMediaItem.MediaType.VINYL, 12); lib.AddLibraryJournal("Journal of C# Goodness", "UofL Journals", 2011, 14, "JJ12 7M", 1, 2, "Information Systems", "Andrew Wright"); lib.AddLibraryJournal("Journal of VB Goodness", "UofL Journals", 2007, 14, "JJ34 3F", 8, 4, "Information Systems", "Alexander Williams"); lib.AddLibraryMagazine("C# Monthly", "UofL Mags", 2010, 14, "MA53 9A", 16, 7); lib.AddLibraryMagazine("C# Monthly", "UofL Mags", 2010, 14, "MA53 9B", 16, 8); lib.AddLibraryMagazine("C# Monthly", "UofL Mags", 2010, 14, "MA53 9C", 16, 9); lib.AddLibraryMagazine("VB Magazine", "UofL Mags", 2011, 14, "MA21 5V", 1, 1); // Add 5 Patrons lib.AddPatron("Ima Reader", "12345"); lib.AddPatron("Jane Doe", "11223"); lib.AddPatron("John Smith", "54321"); lib.AddPatron("James T. Kirk", "98765"); lib.AddPatron("Jean-Luc Picard", "33456"); }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { //object for deserializing in binary format System.Runtime.Serialization.Formatters.Binary.BinaryFormatter reader = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); DialogResult result; //result of OpenFileDialog string fileName; // name of file containing data using (OpenFileDialog filechooser = new OpenFileDialog()) { result = filechooser.ShowDialog(); fileName = filechooser.FileName; //get specified name } if (result == DialogResult.OK) { try { if (fileName == string.Empty) MessageBox.Show("Invalid File Name"); else { input = new FileStream(fileName, FileMode.Open, FileAccess.Read); //create file stream to allow read access from file lib = (Library)reader.Deserialize(input); //store deserialized file in lib variable } } catch (System.Runtime.Serialization.SerializationException) { MessageBox.Show("No more records in file."); } finally { if (input != null) input.Close(); } } }