diff --git a/.vs/Password Manager/DesignTimeBuild/.dtbcache.v2 b/.vs/Password Manager/DesignTimeBuild/.dtbcache.v2 index cea98f0..659f5cc 100644 Binary files a/.vs/Password Manager/DesignTimeBuild/.dtbcache.v2 and b/.vs/Password Manager/DesignTimeBuild/.dtbcache.v2 differ diff --git a/.vs/Password Manager/v17/.suo b/.vs/Password Manager/v17/.suo index 239d30e..51f1b4e 100644 Binary files a/.vs/Password Manager/v17/.suo and b/.vs/Password Manager/v17/.suo differ diff --git a/Password Manager/Fields.cs b/Password Manager/Fields.cs deleted file mode 100644 index e9d82c3..0000000 --- a/Password Manager/Fields.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace Password_Manager -{ - static class Fields - { - public static Profile CurrentProfile; - public static ProfileList ListOfProfiles; - } -} diff --git a/Password Manager/MainForm.Designer.cs b/Password Manager/MainForm.Designer.cs index a8d5bb5..cf66e95 100644 --- a/Password Manager/MainForm.Designer.cs +++ b/Password Manager/MainForm.Designer.cs @@ -42,19 +42,20 @@ searchBox.PlaceholderText = "Search for a password"; searchBox.Size = new Size(257, 27); searchBox.TabIndex = 0; - searchBox.TextChanged += UpdateResultList; + searchBox.TextChanged += resultList.ReloadResults; // // resultList // resultList.FormattingEnabled = true; resultList.ItemHeight = 20; - resultList.Location = new Point(12, 99); + resultList.Location = new Point(12, 60); resultList.Name = "resultList"; - resultList.Size = new Size(150, 104); + resultList.Size = new Size(257, 364); resultList.TabIndex = 1; // // profileSelection // + profileSelection.DisplayMember = "Name"; profileSelection.DropDownHeight = 100; profileSelection.DropDownWidth = 200; profileSelection.FormattingEnabled = true; @@ -63,7 +64,7 @@ profileSelection.Name = "profileSelection"; profileSelection.Size = new Size(200, 28); profileSelection.TabIndex = 2; - profileSelection.SelectedIndexChanged += ClearSearchBox; + profileSelection.SelectedIndexChanged += ProfileHandler.ProfileChange; // // addProfile // @@ -74,12 +75,11 @@ addProfile.Text = "Add"; addProfile.UseVisualStyleBackColor = true; addProfile.Click += AddProfile; - // - // button2 + // removeProfile // removeProfile.Location = new Point(691, 60); - removeProfile.Name = "button2"; + removeProfile.Name = "removeProfile"; removeProfile.Size = new Size(95, 29); removeProfile.TabIndex = 4; removeProfile.Text = "Delete"; @@ -96,7 +96,7 @@ Controls.Add(resultList); Controls.Add(searchBox); Name = "MainForm"; - Text = "Form1"; + Text = "Password Manager"; ResumeLayout(false); PerformLayout(); } @@ -106,7 +106,7 @@ public TextBox searchBox; public ResultListBox resultList; public ComboBox profileSelection; - private Button addProfile; - private Button removeProfile; + public Button addProfile; + public Button removeProfile; } } \ No newline at end of file diff --git a/Password Manager/MainForm.cs b/Password Manager/MainForm.cs index c297d1f..d33ce4c 100644 --- a/Password Manager/MainForm.cs +++ b/Password Manager/MainForm.cs @@ -10,23 +10,9 @@ namespace Password_Manager private void ChangeProfile(object sender, EventArgs e) { ComboBox cb = (ComboBox)sender; - Fields.CurrentProfile = Fields.ListOfProfiles.SearchByName(cb.Text); - } - - private void ClearSearchBox(object sender, EventArgs e) //needed because declaring an anonymous method and subscribing that onto the delegate didn't work for some reason - { + ProfileHandler.CurrentProfile = ProfileHandler.ListOfProfiles.SearchByName(cb.Text); searchBox.Clear(); - } - - private void UpdateResultList(object sender, EventArgs args) - { - resultList.Refresh(); - } - - private void ProfileChange(object sender, EventArgs e) - { - searchBox.Clear(); - resultList.Refresh(); + this.Text = ProfileHandler.CurrentProfile == null ? "Password Manager" : $"Current profile: {ProfileHandler.CurrentProfile.Name}"; } private void AddProfile(object sender, EventArgs e) diff --git a/Password Manager/NewProfileForm.Designer.cs b/Password Manager/NewProfileForm.Designer.cs index 9aff248..4461b36 100644 --- a/Password Manager/NewProfileForm.Designer.cs +++ b/Password Manager/NewProfileForm.Designer.cs @@ -53,6 +53,7 @@ button2.TabIndex = 1; button2.Text = "Save"; button2.UseVisualStyleBackColor = true; + button2.Click += Save; // // button3 // @@ -62,19 +63,20 @@ button3.TabIndex = 2; button3.Text = "Cancel"; button3.UseVisualStyleBackColor = true; + button3.Click += Cancel; // - // textBox1 + // nameTextBox // nameTextBox.Location = new Point(12, 12); - nameTextBox.Name = "textBox1"; + nameTextBox.Name = "nameTextBox"; nameTextBox.PlaceholderText = "Work"; nameTextBox.Size = new Size(194, 27); nameTextBox.TabIndex = 3; // - // textBox2 + // pathTextBox // pathTextBox.Location = new Point(12, 45); - pathTextBox.Name = "textBox2"; + pathTextBox.Name = "pathTextBox"; pathTextBox.PlaceholderText = "C:\\Passwords"; pathTextBox.Size = new Size(194, 27); pathTextBox.TabIndex = 4; @@ -90,7 +92,7 @@ Controls.Add(button2); Controls.Add(button1); Name = "NewProfileForm"; - Text = "NewProfileForm"; + Text = "Create profile"; ResumeLayout(false); PerformLayout(); } diff --git a/Password Manager/NewProfileForm.cs b/Password Manager/NewProfileForm.cs index d3202ba..7b0407b 100644 --- a/Password Manager/NewProfileForm.cs +++ b/Password Manager/NewProfileForm.cs @@ -18,12 +18,33 @@ namespace Password_Manager } private void ChooseFolder(object sender, EventArgs e) - { + { FolderBrowserDialog dialog = new FolderBrowserDialog(); if (dialog.ShowDialog() == DialogResult.OK) { pathTextBox.Text = dialog.SelectedPath; } } + + private void Save(object sender, EventArgs e) + { + if (nameTextBox.Text != "" && pathTextBox.Text != "") + { + Profile profile = new Profile(nameTextBox.Text, pathTextBox.Text); + ProfileHandler.ListOfProfiles.Add(profile); + ProfileHandler.CurrentProfile = profile; + Program.mainForm.resultList.ReloadResults(); + this.Close(); + } + else + { + MessageBox.Show("You must fill in all fields to continue.", "Error: Empty fields", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void Cancel(object sender, EventArgs e) + { + this.Close(); + } } } diff --git a/Password Manager/ProfileHandler.cs b/Password Manager/ProfileHandler.cs new file mode 100644 index 0000000..dfdc3cd --- /dev/null +++ b/Password Manager/ProfileHandler.cs @@ -0,0 +1,16 @@ +using System; + +namespace Password_Manager +{ + static class ProfileHandler + { + public static Profile CurrentProfile; + public static ProfileList ListOfProfiles; + + public static void ProfileChange(object sender, EventArgs e) + { + Program.mainForm.searchBox.Clear(); + Program.mainForm.resultList.ReloadResults(); + } + } +} diff --git a/Password Manager/Program.cs b/Password Manager/Program.cs index 30bed6f..f0634cd 100644 --- a/Password Manager/Program.cs +++ b/Password Manager/Program.cs @@ -8,9 +8,11 @@ namespace Password_Manager [STAThread] static void Main() { - Fields.ListOfProfiles = new ProfileList(); + ProfileHandler.ListOfProfiles = new ProfileList(); ApplicationConfiguration.Initialize(); - Application.Run(new MainForm()); - } + Application.Run(mainForm); + } + + public static MainForm mainForm = new MainForm(); } } \ No newline at end of file diff --git a/Password Manager/ResultListBox.cs b/Password Manager/ResultListBox.cs index 0a31a23..1cb2027 100644 --- a/Password Manager/ResultListBox.cs +++ b/Password Manager/ResultListBox.cs @@ -6,18 +6,35 @@ namespace Password_Manager { public ResultListBox() : base() { } - public void Refresh() + public void ReloadResults() { - DirectoryInfo d = new DirectoryInfo(Fields.CurrentProfile.Path); //Assuming Test is your Folder + DirectoryInfo d = new DirectoryInfo(ProfileHandler.CurrentProfile.Path); FileInfo[] files = d.GetFiles("*.gpg"); - string[] elements = new string[files.Length]; - - for (int i = 0; i < elements.Length; i++) + string[] arrayTmp = new string[files.Length]; + for (int i = 0; i < arrayTmp.Length; i++) { - elements[i] = files[i].Name; + arrayTmp[i] = files[i].Name; + } + List elements = new List(arrayTmp); + + string[] copy = elements.ToArray(); + if (Program.mainForm.searchBox.Text != "") //we have a search query + { + foreach (string s in copy) + { + if (!s.Contains(Program.mainForm.searchBox.Text)) + { + elements.Remove(s); + } + } } this.DataSource = elements; } + + public void ReloadResults(object o, EventArgs e) //needed so that I can subscribe this method to a delegate in MainForm + { + ReloadResults(); + } } }