Example #1
0
        /// <summary>
        /// Event for the Save As button on the File menu. Opens a SaveFileDialog and saves to the chosen destination.
        /// </summary>
        private void saveAsMenuItem_Click(object sender, EventArgs e)
        {
            if (heroMode) // HERO MODE silliness
            {
                var result =
                    MessageBox.Show(
                        @"WARNING:\nHERO MODE is enabled. Are you sure you want to use your ONE save on the spreadsheet right now?",
                        @"THE GREAT HERO MODE SAVE", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.No)
                {
                    return;
                }
            }
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter           = @"Spreadsheet Files (*.sprd)|*.sprd|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex      = 1;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                String filename = saveFileDialog1.FileName;
                try
                {
                    ourSpreadsheet.Save(filename);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
                filepath     = filename;
                documentName = Path.GetFileNameWithoutExtension(filename);
                saveToolStripMenuItem1.Enabled = false;
                this.Text       = documentName + @"- " + applicationName;
                documentChanged = false;
                if (heroMode) //More HERO MODE ridiculousness.
                {
                    heroModeSaved          = true;
                    saveAsMenuItem.Enabled = false;
                }
            }
        }
        /// <summary>
        /// Method for handling the SaveAs button click.
        /// This method will open a SaveAs dialog box for the user to select where they want to save the file.
        /// Once the user presses Save, the spreadsheet will be saved to the XML file they specified
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Create new SaveFileDialog object
            SaveFileDialog saveAs = new SaveFileDialog();

            // Try to perform the save process
            try
            {
                // Set the saveAs filter to show just .ss files by default, but give option to allow all files to be shown
                saveAs.Filter = "Spreadsheet File (*.ss)|*.ss|All Files |*.*";
                // Set the default file view
                saveAs.FilterIndex      = 1;
                saveAs.RestoreDirectory = true;

                // If the user presses save, perform the file writing
                if (saveAs.ShowDialog() == DialogResult.OK)
                {
                    String xml = "";
                    // Create a new StreamWriter and file for writing the object
                    using (StreamWriter writer = File.CreateText(saveAs.FileName))
                    {
                        // Write the spreadsheet
                        spreadSheet.Save(writer);
                        xml = writer.ToString();
                        // Set the instance variable to remember the file name
                        currentFileName = saveAs.FileName;
                        // Close the file writer
                        writer.Close();
                    }
                }
            }
            // Catch the save exceptions and show a dialog box with the error
            catch (IOException ioexception)
            {
                MessageBox.Show("Problem saving file" + ioexception.Message);
            }
        }