public void AddAccount(string description, string username, string password) { PassStoreAccount account = new PassStoreAccount(description, username, password); accountList.Insert(0, account); SaveData(); }
// delete selected accounts from the grid view void BtnDeleteAccountsClick(object sender, EventArgs e) { List <DataGridViewRow> selected = SelectedAccounts(); if (selected.Count == 0) { MessageBox.Show("Please select the account(s) you'd like to delete.", "Password Storage", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } DialogResult confirm = MessageBox.Show("Are you sure you want to continue?\nThis action cannot be reversed.\n" + selected.Count + " accounts will be deleted.", "Password Storage", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (confirm != DialogResult.Yes) { return; } List <PassStoreAccount> accounts = passStore.GetAccounts(); List <PassStoreAccount> accountsToRemove = new List <PassStoreAccount>(); foreach (DataGridViewRow row in selected) { int index = row.Index; PassStoreAccount toRemove = accounts[index]; accountsToRemove.Add(toRemove); } foreach (PassStoreAccount account in accountsToRemove) { passStore.DeleteAccount(account); } UpdateAccounts(); }
// export selected accounts to clipboard (format: description - username - password) void BtnExportAccountsClick(object sender, EventArgs e) { List <DataGridViewRow> selected = SelectedAccounts(); int total = selected.Count; // how many selected rows there are if (total == 0) { MessageBox.Show("Please select the account(s) you'd like to export.", "Password Storage", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } List <PassStoreAccount> accounts = passStore.GetAccounts(); string data = string.Empty; foreach (DataGridViewRow row in selected) { int index = row.Index; PassStoreAccount account = accounts[index]; string description = account.GetDescription(); string username = account.GetUsername(); string password = account.GetPassword(); data += description + " - " + username + " - " + password; if (row != selected[total - 1]) { // new line if it's not the last item data += Environment.NewLine; } } try { Clipboard.SetText(data); MessageBox.Show(total + " account(s) have been copied to your clipboard.", "Password Storage", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception) { MessageBox.Show("Failed to copy information to clipboard.", "Password Storage", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
void BtnCopyPasswordClick(object sender, EventArgs e) { List <DataGridViewRow> selected = SelectedAccounts(); if (selected.Count != 1) { MessageBox.Show("Please select one account.", "Password Storage", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } PassStoreAccount account = passStore.GetAccounts()[selected[0].Index]; try { Clipboard.SetText(account.GetPassword()); } catch (Exception) { MessageBox.Show("Failed to copy information to clipboard.", "Password Storage", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void UpdateAccounts() { accountList = new List <PassStoreAccount>(); string data = GetData(); if (string.IsNullOrEmpty(data)) { return; } // split by line break string[] lines = data.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); foreach (string line in lines) { if (string.IsNullOrWhiteSpace(line)) { continue; } string[] accountData = line.Split(new[] { splitter }, StringSplitOptions.None); PassStoreAccount account = new PassStoreAccount(accountData[0], accountData[1], accountData[2]); accountList.Add(account); } }
public void DeleteAccount(PassStoreAccount account) { accountList.Remove(account); SaveData(); }