コード例 #1
0
ファイル: Main.cs プロジェクト: nathandurst/my-library
        /********************************************************************
         *  Event handler for the File>Save menu item
         *      will take the records that are currently being accessed and write them
         *      to the file with the name of the media.
         **********************************************************************/
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Gets the active child form to determine if it is already opened or not
            RecordsForm R = this.ActiveMdiChild as RecordsForm;

            if (R == null)
            {
                MessageBox.Show("No form is currently displayed.", "Error");
            }

            //Since there is a form open, we can now write the records to a file
            else
            {
                //Define variables needed for writing
                string file;
                media   = R.getMedia();
                file    = media + ".inv";
                Records = R.getRecords();

                //Write to file
                write = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
                if (Records.Count > 0)
                {
                    bf.Serialize(write, Records);
                }
                write.Close();
                MessageBox.Show("Records have been saved to " + file, "Done!");
            }
        }
コード例 #2
0
ファイル: Main.cs プロジェクト: nathandurst/my-library
        /********************************************************************
         *  Loads the records into the Records variable. Does not return or require any variables
         *      Using FileStream and BinaryFormatter, this function reads from the specified file
         *      and loads the records from that file into the Records variable where they
         *      can then be used throughout the program.
         **********************************************************************/
        private void loadRecords()
        {
            //Gets the active child form to determine if it is already opened or not
            RecordsForm R           = this.ActiveMdiChild as RecordsForm;
            int         numChildren = this.MdiChildren.Length;

            if (R != null || numChildren == 2)
            {
                MessageBox.Show("Only one form at a time, please.", "Error");
            }
            //Since it is not already openened, the function will read the records from the file and
            //  display them on a new RecordsForm
            else
            {
                //Shows a new records form (blank)
                R           = new RecordsForm(Records, media);
                R.MdiParent = this;
                R.Show();

                //Determines which media we are wanting and reads form the corresponding file
                string file = R.getMedia() + ".inv";
                Records.Clear(); //Clears the records in case we are using a different media
                read = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read);
                if (read.Length > 0)
                {
                    Records = bf.Deserialize(read) as List <Record>;
                }

                //Calls the refresh function which displays the records on the RecordsForm
                R.refresh(Records);
                read.Close();
            }
        }