This repository has been archived on 2025-09-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Password-Manager-Legacy/Password Manager/PasswordListBox.cs

74 lines
2.4 KiB
C#
Raw Normal View History

2023-03-27 22:13:40 +02:00
using System.Collections;
namespace Password_Manager
2023-03-27 00:19:30 +02:00
{
public delegate string SearchQuery();
public class PasswordListBox : ListBox
{
public event DataRequest? PathRequest;
2023-03-27 00:19:30 +02:00
public event SearchQuery? SearchQueryRequest;
public PasswordListBox(DataRequest PathRequest, SearchQuery? SearchQueryRequest = null) : base()
{
this.PathRequest = PathRequest;
this.SearchQueryRequest = SearchQueryRequest;
}
2023-03-27 00:19:30 +02:00
public PasswordListBox() : base() { }
/*do not instantiate this class using this constructor if you want to use the ReloadResults method afterwards.
Use this so that the MainForm.Designer.cs file doesn't have a stroke due to Event References, then re-instantiate the object
afterwards using the other constructor*/
2023-03-27 00:19:30 +02:00
public void ReloadResults(object? sender = null, EventArgs? arg = null)
2023-03-27 00:19:30 +02:00
{
DirectoryInfo d;
FileInfo[] files = new FileInfo[0];
try
{
if (PathRequest != null)
{
d = new DirectoryInfo(PathRequest());
}
else
{
2023-03-27 22:13:40 +02:00
throw new InvalidOperationException("You cannot use the ReloadResults method if you instantiated the object using the parameterless constructor");
}
2023-03-27 00:19:30 +02:00
files = d.GetFiles("*.gpg");
2023-03-27 22:13:40 +02:00
}
catch (ArgumentNullException e)
2023-03-27 00:19:30 +02:00
{
MessageBox.Show(e.ToString(), "Error: Invalid path", MessageBoxButtons.OK, MessageBoxIcon.Error);
2023-03-27 00:19:30 +02:00
}
2023-03-27 22:13:40 +02:00
List<string> elements = new List<string>();
for (int i = 0; i < files.Length; i++)
2023-03-27 00:19:30 +02:00
{
2023-03-27 22:13:40 +02:00
elements.Add(files[i].Name);
2023-03-27 00:19:30 +02:00
}
string[] copy = elements.ToArray();
2023-03-27 22:13:40 +02:00
string? searchQuery = SearchQueryRequest?.Invoke();
if (searchQuery != null) //we have a search query
2023-03-27 00:19:30 +02:00
{
foreach (string s in copy)
{
if (!s.Contains(searchQuery))
{
elements.Remove(s);
}
}
}
this.DataSource = elements;
2023-03-28 08:22:08 +02:00
try
{
SelectedIndex = 0;
} catch (ArgumentOutOfRangeException)
{
SelectedIndex = -1;
}
2023-03-27 00:19:30 +02:00
}
}
}