Esempio n. 1
0
        //Event Handler for opening an existing spreadsheet
        // added an additional parameter for 'Piping' in the FileName
        private void openMenuItem_Click(object sender, EventArgs e, SpreadsheetPanel panel, ref string FileName)
        {
            OpenFileDialog newDialog = new OpenFileDialog();

            newDialog.Filter = "Spreadsheet files (*.sprd)|*.sprd|All files (*.*)|*.*";
            if (newDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if (panel.getSS().Changed)
                    {
                        // ask if the spreadsheet should be overridden
                        DialogResult r = MessageBox.Show("Do you want to save your changes?", "Save?", MessageBoxButtons.YesNoCancel);
                        if (r == DialogResult.Yes)
                        {
                            saveMenuItem.PerformClick();

                            foreach (string cell in panel.getSS().GetNamesOfAllNonemptyCells())
                            {
                                int col, row;
                                cellAddress(cell, out col, out row);
                                panel.SetValue(col, row, "");
                            }

                            FileName = newDialog.FileName; //updating the FileName
                            panel.setSS(new Spreadsheet(FileName, validator, s => s.ToUpper(), "ps6"));
                            OpeningSpreadsheet.RunWorkerAsync();
                            this.Text = FileName.Substring(FileName.LastIndexOf("\\") + 1);
                        }
                        else if (r == DialogResult.No)
                        {
                            foreach (string cell in panel.getSS().GetNamesOfAllNonemptyCells())
                            {
                                int col, row;
                                cellAddress(cell, out col, out row);
                                panel.SetValue(col, row, "");
                            }

                            FileName = newDialog.FileName; //updating the FileName
                            panel.setSS(new Spreadsheet(FileName, validator, s => s.ToUpper(), "ps6"));
                            OpeningSpreadsheet.RunWorkerAsync();
                            this.Text = FileName.Substring(FileName.LastIndexOf("\\") + 1);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Unable to open file");
                }
            }
        }