private void MenuFileNew_Click(object sender, EventArgs e) { // TODO: Add safety functions like "are you sure" and "unsaved data" this.data = new PasswordStorage(); data.DataSourceChanged += Data_DataSourceChanged; RefreshDataGridView(); }
private void MenuFileLoad_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Title = "Load file ..."; ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); ofd.CheckPathExists = true; StringBuilder filters = new StringBuilder(); { filters.Append(this.Text); filters.Append(" files ("); filters.Append("*" + PW_FILE_EXTENSION); filters.Append(")|"); filters.Append("*" + PW_FILE_EXTENSION); } ofd.Filter = filters.ToString(); // ofd.FilterIndex doesnt need to be set here (default = 1) var result = ofd.ShowDialog(); string FileNameAndPath = ofd.FileName; bool ok = FileNameAndPath.EndsWith(PW_FILE_EXTENSION); if (result != DialogResult.OK) { return; } else if (!ok) { _ = MessageBox.Show(String.Format("Can't load files with the file extension \"{0}\".", System.IO.Path.GetExtension(FileNameAndPath)), "File loading error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else if ((ofd.FileNames.Length > 1)) { _ = MessageBox.Show("Something went wrong while trying to access the file", "File access error", MessageBoxButtons.OK, MessageBoxIcon.Error); DefaultSaveFileNameAndPath = String.Empty; return; } try { data = PasswordStorage.DeSerialize(FileNameAndPath); dataGrid.Source = data.GetPasswordDataTable(); } catch (Exception) { _ = MessageBox.Show("Something went wrong while trying to read the file", "File read error", MessageBoxButtons.OK, MessageBoxIcon.Error); DefaultSaveFileNameAndPath = String.Empty; return; } // File path is valid, so fast saving should be possible DefaultSaveFileNameAndPath = FileNameAndPath; justSaved = true; MenuFileSave.Enabled = !justSaved; } }
private void MainForm_Load(object sender, EventArgs e) { InitializeGUI(); // Create PasswordStorage data = new PasswordStorage(); data.DataSourceChanged += Data_DataSourceChanged; InitializeDataGridView(); InitializeMainSplitContainer(); this.dataGrid.StickToParentBoundaries(); this.KeyDown += MainForm_KeyDown; this.dataGrid.KeyDown += MainForm_KeyDown; // ... }