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/GUI/PasswordListBox.cs

73 lines
2.2 KiB
C#
Raw Normal View History

2023-03-27 22:13:40 +02:00
using System.Collections;
2023-04-02 22:10:42 +02:00
namespace GUI;
public delegate string SearchQuery();
public class PasswordListBox : ListBox
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +02:00
public event DataRequest? PathRequest;
public event SearchQuery? SearchQueryRequest;
2023-03-27 00:19:30 +02:00
2023-04-02 22:10:42 +02:00
public PasswordListBox(DataRequest PathRequest, SearchQuery? SearchQueryRequest = null) : base()
{
this.PathRequest = PathRequest;
this.SearchQueryRequest = SearchQueryRequest;
}
2023-04-02 22:10:42 +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
2023-04-02 22:10:42 +02:00
public void ReloadResults(object? sender = null, EventArgs? arg = null)
{
DirectoryInfo d;
FileInfo[] files = new FileInfo[0];
try
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +02:00
if (PathRequest != null)
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +02:00
d = new DirectoryInfo(PathRequest());
2023-03-27 22:13:40 +02:00
}
2023-04-02 22:10:42 +02:00
else
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +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
}
2023-04-02 22:10:42 +02:00
files = d.GetFiles("*.gpg");
}
catch (ArgumentNullException e)
{
MessageBox.Show(e.ToString(), "Error: Invalid path", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2023-03-27 00:19:30 +02:00
2023-04-02 22:10:42 +02:00
List<string> elements = new List<string>();
for (int i = 0; i < files.Length; i++)
{
elements.Add(files[i].Name);
}
2023-03-27 00:19:30 +02:00
2023-04-02 22:10:42 +02:00
string[] copy = elements.ToArray();
2023-03-27 22:13:40 +02:00
2023-04-02 22:10:42 +02:00
string? searchQuery = SearchQueryRequest?.Invoke();
if (searchQuery != null) //we have a search query
{
foreach (string s in copy)
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +02:00
if (!s.Contains(searchQuery))
2023-03-27 00:19:30 +02:00
{
2023-04-02 22:10:42 +02:00
elements.Remove(s);
2023-03-27 00:19:30 +02:00
}
}
2023-04-02 22:10:42 +02:00
}
2023-03-27 00:19:30 +02:00
2023-04-02 22:10:42 +02:00
this.DataSource = elements;
try
{
SelectedIndex = 0;
} catch (ArgumentOutOfRangeException)
{
SelectedIndex = -1;
2023-03-27 00:19:30 +02:00
}
}
}