I'm literally on fire rn

This commit is contained in:
Miskolczi Richárd
2023-03-24 14:03:09 +01:00
parent b06568fb27
commit 99498ed660
10 changed files with 85 additions and 51 deletions

Binary file not shown.

View File

@@ -1,10 +0,0 @@
using System;
namespace Password_Manager
{
static class Fields
{
public static Profile CurrentProfile;
public static ProfileList ListOfProfiles;
}
}

View File

@@ -42,19 +42,20 @@
searchBox.PlaceholderText = "Search for a password"; searchBox.PlaceholderText = "Search for a password";
searchBox.Size = new Size(257, 27); searchBox.Size = new Size(257, 27);
searchBox.TabIndex = 0; searchBox.TabIndex = 0;
searchBox.TextChanged += UpdateResultList; searchBox.TextChanged += resultList.ReloadResults;
// //
// resultList // resultList
// //
resultList.FormattingEnabled = true; resultList.FormattingEnabled = true;
resultList.ItemHeight = 20; resultList.ItemHeight = 20;
resultList.Location = new Point(12, 99); resultList.Location = new Point(12, 60);
resultList.Name = "resultList"; resultList.Name = "resultList";
resultList.Size = new Size(150, 104); resultList.Size = new Size(257, 364);
resultList.TabIndex = 1; resultList.TabIndex = 1;
// //
// profileSelection // profileSelection
// //
profileSelection.DisplayMember = "Name";
profileSelection.DropDownHeight = 100; profileSelection.DropDownHeight = 100;
profileSelection.DropDownWidth = 200; profileSelection.DropDownWidth = 200;
profileSelection.FormattingEnabled = true; profileSelection.FormattingEnabled = true;
@@ -63,7 +64,7 @@
profileSelection.Name = "profileSelection"; profileSelection.Name = "profileSelection";
profileSelection.Size = new Size(200, 28); profileSelection.Size = new Size(200, 28);
profileSelection.TabIndex = 2; profileSelection.TabIndex = 2;
profileSelection.SelectedIndexChanged += ClearSearchBox; profileSelection.SelectedIndexChanged += ProfileHandler.ProfileChange;
// //
// addProfile // addProfile
// //
@@ -74,12 +75,11 @@
addProfile.Text = "Add"; addProfile.Text = "Add";
addProfile.UseVisualStyleBackColor = true; addProfile.UseVisualStyleBackColor = true;
addProfile.Click += AddProfile; addProfile.Click += AddProfile;
// //
// button2 // removeProfile
// //
removeProfile.Location = new Point(691, 60); removeProfile.Location = new Point(691, 60);
removeProfile.Name = "button2"; removeProfile.Name = "removeProfile";
removeProfile.Size = new Size(95, 29); removeProfile.Size = new Size(95, 29);
removeProfile.TabIndex = 4; removeProfile.TabIndex = 4;
removeProfile.Text = "Delete"; removeProfile.Text = "Delete";
@@ -96,7 +96,7 @@
Controls.Add(resultList); Controls.Add(resultList);
Controls.Add(searchBox); Controls.Add(searchBox);
Name = "MainForm"; Name = "MainForm";
Text = "Form1"; Text = "Password Manager";
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
} }
@@ -106,7 +106,7 @@
public TextBox searchBox; public TextBox searchBox;
public ResultListBox resultList; public ResultListBox resultList;
public ComboBox profileSelection; public ComboBox profileSelection;
private Button addProfile; public Button addProfile;
private Button removeProfile; public Button removeProfile;
} }
} }

View File

@@ -10,23 +10,9 @@ namespace Password_Manager
private void ChangeProfile(object sender, EventArgs e) private void ChangeProfile(object sender, EventArgs e)
{ {
ComboBox cb = (ComboBox)sender; ComboBox cb = (ComboBox)sender;
Fields.CurrentProfile = Fields.ListOfProfiles.SearchByName(cb.Text); ProfileHandler.CurrentProfile = ProfileHandler.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
{
searchBox.Clear(); searchBox.Clear();
} this.Text = ProfileHandler.CurrentProfile == null ? "Password Manager" : $"Current profile: {ProfileHandler.CurrentProfile.Name}";
private void UpdateResultList(object sender, EventArgs args)
{
resultList.Refresh();
}
private void ProfileChange(object sender, EventArgs e)
{
searchBox.Clear();
resultList.Refresh();
} }
private void AddProfile(object sender, EventArgs e) private void AddProfile(object sender, EventArgs e)

View File

@@ -53,6 +53,7 @@
button2.TabIndex = 1; button2.TabIndex = 1;
button2.Text = "Save"; button2.Text = "Save";
button2.UseVisualStyleBackColor = true; button2.UseVisualStyleBackColor = true;
button2.Click += Save;
// //
// button3 // button3
// //
@@ -62,19 +63,20 @@
button3.TabIndex = 2; button3.TabIndex = 2;
button3.Text = "Cancel"; button3.Text = "Cancel";
button3.UseVisualStyleBackColor = true; button3.UseVisualStyleBackColor = true;
button3.Click += Cancel;
// //
// textBox1 // nameTextBox
// //
nameTextBox.Location = new Point(12, 12); nameTextBox.Location = new Point(12, 12);
nameTextBox.Name = "textBox1"; nameTextBox.Name = "nameTextBox";
nameTextBox.PlaceholderText = "Work"; nameTextBox.PlaceholderText = "Work";
nameTextBox.Size = new Size(194, 27); nameTextBox.Size = new Size(194, 27);
nameTextBox.TabIndex = 3; nameTextBox.TabIndex = 3;
// //
// textBox2 // pathTextBox
// //
pathTextBox.Location = new Point(12, 45); pathTextBox.Location = new Point(12, 45);
pathTextBox.Name = "textBox2"; pathTextBox.Name = "pathTextBox";
pathTextBox.PlaceholderText = "C:\\Passwords"; pathTextBox.PlaceholderText = "C:\\Passwords";
pathTextBox.Size = new Size(194, 27); pathTextBox.Size = new Size(194, 27);
pathTextBox.TabIndex = 4; pathTextBox.TabIndex = 4;
@@ -90,7 +92,7 @@
Controls.Add(button2); Controls.Add(button2);
Controls.Add(button1); Controls.Add(button1);
Name = "NewProfileForm"; Name = "NewProfileForm";
Text = "NewProfileForm"; Text = "Create profile";
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();
} }

View File

@@ -25,5 +25,26 @@ namespace Password_Manager
pathTextBox.Text = dialog.SelectedPath; 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();
}
} }
} }

View File

@@ -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();
}
}
}

View File

@@ -8,9 +8,11 @@ namespace Password_Manager
[STAThread] [STAThread]
static void Main() static void Main()
{ {
Fields.ListOfProfiles = new ProfileList(); ProfileHandler.ListOfProfiles = new ProfileList();
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new MainForm()); Application.Run(mainForm);
} }
public static MainForm mainForm = new MainForm();
} }
} }

View File

@@ -6,18 +6,35 @@ namespace Password_Manager
{ {
public ResultListBox() : base() { } 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"); FileInfo[] files = d.GetFiles("*.gpg");
string[] elements = new string[files.Length]; string[] arrayTmp = new string[files.Length];
for (int i = 0; i < arrayTmp.Length; i++)
for (int i = 0; i < elements.Length; i++)
{ {
elements[i] = files[i].Name; arrayTmp[i] = files[i].Name;
}
List<string> elements = new List<string>(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; this.DataSource = elements;
} }
public void ReloadResults(object o, EventArgs e) //needed so that I can subscribe this method to a delegate in MainForm
{
ReloadResults();
}
} }
} }