예제 #1
0
        /// <summary>
        /// Opens dialog for user to load their spreadsheet
        /// </summary>
        internal void load(SpreadsheetPanel ss)
        {
            //If current sheet has been changed, prompt the user if they are sure
            if (sheet.Changed)
            {
                if (MessageBox.Show("Spreadsheet isn't saved! Are you sure you want to load another one?", "Unsaved changes", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
            }

            //Clears current sheet
            ss.Clear();

            //Makes open dialog box
            OpenFileDialog openDialog = new OpenFileDialog();

            openDialog.Title            = "Open Spreadsheet";
            openDialog.Filter           = "Spreadsheet files (*.sprd)|*.sprd|All files (*.*)|*.*";
            openDialog.FilterIndex      = 1;
            openDialog.RestoreDirectory = true;

            //Gets file or shows error message
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    //Load spreadsheet, update cells, then returns current box's content, value, and name to form to display
                    sheet.Load(openDialog.FileName);
                    updateCells(sheet, ss, new List <string>(sheet.GetNamesOfAllNonemptyCells()));
                    ss.GetSelection(out int x, out int y);
                    string cellName = coordsToCell(x, y);
                    object contents = sheet.GetCellContents(cellName);
                    object value    = sheet.GetCellValue(cellName);
                    //Gets contents if formula
                    if (contents is Formula)
                    {
                        contents = "=" + contents.ToString();
                    }
                    //Gets value if formula error
                    if (value is FormulaError)
                    {
                        value = "FormulaError";
                    }
                    openDialog.Dispose();
                    form.endCellSelect(contents.ToString(), value.ToString(), cellName);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "Invalid file", MessageBoxButtons.OK);
                    openDialog.Dispose();
                }
            }
        }