private bool SaveToFile() { log.Debug("EditorForm::SaveToFile() {"); if (scoreboard == null) { log.Debug("EditorForm::SaveToFile() } // scoreboard == null"); return(true); } bool cancelled = false; using (SaveFileDialog saveFileDialog = new SaveFileDialog()) { saveFileDialog.Filter = "CSV files (*.csv)|*.csv|JSON files (*.json)|*.json"; saveFileDialog.FilterIndex = 1; saveFileDialog.RestoreDirectory = true; saveFileDialog.DefaultExt = "csv"; saveFileDialog.AddExtension = true; if (saveFileDialog.ShowDialog() == DialogResult.OK) { fileName = saveFileDialog.FileName; switch (Path.GetExtension(fileName)) { default: case ".csv": CsvHandler.SaveToFile(scoreboard, saveFileDialog.OpenFile()); break; case ".json": JsonHandler.SaveToFile(scoreboard, saveFileDialog.OpenFile()); break; } hasUnsavedChanges = false; } else { cancelled = true; } } log.Debug("EditorForm::SaveToFile() }"); return(!cancelled); }
private void LoadFromFile() { log.Debug("EditorForm::LoadFromFile() {"); using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.InitialDirectory = string.IsNullOrEmpty(fileName) ? Environment.SpecialFolder.MyDocuments.ToString() : fileName; openFileDialog.Filter = "CSV files (*.csv)|*.csv|JSON files (*.json)|*.json"; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { fileName = openFileDialog.FileName; switch (Path.GetExtension(fileName)) { case ".csv": scoreboard = CsvHandler.LoadFromFile(openFileDialog.OpenFile()); break; case ".json": scoreboard = JsonHandler.LoadFromFile(openFileDialog.OpenFile()); break; default: throw new ArgumentException("Unknown file format: '" + Path.GetExtension(fileName) + "'."); } hasUnsavedChanges = false; NumericInputAnimationLength.Value = GetSuggestedAnimationLength(); UpdateScores(); UpdateControls(); } } log.Debug("EditorForm::LoadFromFile() }"); }
private void MenuFileNewFromTemplate_Click(object sender, EventArgs e) { log.Debug("EditorForm::MenuFileNewFromTemplate_Click() {"); if (hasUnsavedChanges) { try { DialogResult result = MessageBox.Show("There are unsaved changes. Save?", "Information", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); switch (result) { case DialogResult.Yes: if (!SaveToFile()) { log.Debug("EditorForm::MenuFileNewFromTemplate_Click() } // cancelled (save existing)"); return; } break; case DialogResult.No: break; default: case DialogResult.Cancel: log.Debug("EditorForm::MenuFileNewFromTemplate_Click() } // cancelled (save existing)"); return; } } catch (Exception ex) { log.Error("Failed to import template: Failed to save existing scoreboard.", ex); MessageBox.Show("Failed to import template: Failed to save existing scoreboard: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); log.Debug("EditorForm::MenuFileNewFromTemplate_Click() }"); return; } } try { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.InitialDirectory = string.IsNullOrEmpty(fileName) ? Environment.SpecialFolder.MyDocuments.ToString() : fileName; openFileDialog.Filter = "CSV files (*.csv)|*.csv|JSON files (*.json)|*.json"; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; Scoreboard template; if (openFileDialog.ShowDialog() == DialogResult.OK) { fileName = openFileDialog.FileName; switch (Path.GetExtension(fileName)) { case ".csv": template = CsvHandler.LoadFromFile(openFileDialog.OpenFile()); break; case ".json": template = JsonHandler.LoadFromFile(openFileDialog.OpenFile()); break; default: throw new ArgumentException("Unknown file format: '" + Path.GetExtension(fileName) + "'."); } hasUnsavedChanges = false; scoreboard = new Scoreboard(template.ExpectedNumberOfTeams, template.ExpectedNumberOfCategories); foreach (string team in template.Teams) { scoreboard.AddTeam(team); } NumericInputAnimationLength.Value = GetSuggestedAnimationLength(); UpdateScores(); UpdateControls(); } } } catch (Exception ex) { log.Error("Failed to import template.", ex); MessageBox.Show("Failed to import template: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } log.Debug("EditorForm::MenuFileNewFromTemplate_Click() }"); }