I am currently trying to implement a search function into a simple windows form app. Currently the app allows you to add a new record into a text file, and sort said text file which is displayed in a listbox.
I now want to be able to search said listbox and display the result however can't think of a way of how to do it, so any advice or any changes I need to make and add would be great.
I am currently using visual studio and writing in C#.
This is also my form code:
namespace RAApplication
{
public partial class Form1 : Form
{
private Account[] accessList = new Account[1];
public Form1()
{
InitializeComponent();
}
private void Write(Account obj)
{
StreamWriter sw = new StreamWriter("Accounts.txt"); //Writes into Text File ACCOUNTS
sw.WriteLine(accessList.Length + 1 );
sw.WriteLine(obj.ParentAccount);
sw.WriteLine(obj.ChildAccount);
sw.WriteLine(obj.Access);
for (int x = 0; x < accessList.Length; x++) //Re-Writes the previous info from text file
{
sw.WriteLine(accessList[x].ParentAccount);
sw.WriteLine(accessList[x].ChildAccount);
sw.WriteLine(accessList[x].Access);
}
sw.Close(); //Closed text file
}
private void Read()
{
StreamReader sr = new StreamReader("Accounts.txt");
accessList = new Account[Convert.ToInt32(sr.ReadLine())];
for (int x = 0; x < accessList.Length; x++)
{
accessList[x] = new Account();
accessList[x].ParentAccount = sr.ReadLine();
accessList[x].ChildAccount = sr.ReadLine();
accessList[x].Access = sr.ReadLine();
}//Loops through all info in text file
sr.Close();
}
private void Display() // writes to txtAccess list box
{
lstRADisplay.Items.Clear();
for(int x = 0; x < accessList.Length; x++)
{
lstRADisplay.Items.Add(accessList[x].ToString());
}
}
private void ClearForm()
{
txtParent.Text = String.Empty;
txtChild.Text = String.Empty;
txtAccess.Text = String.Empty;
}
private void btnCreate_Click(object sender, EventArgs e)
{
Account obj = new Account();
obj.ParentAccount = txtParent.Text;
obj.ChildAccount = txtChild.Text;
obj.Access = txtAccess.Text;
Write(obj);
Sort(); //Sorts each time reading file
Read();
Display();
ClearForm();
}
private void Form1_Load(object sender, EventArgs e)
{
Read(); //Read Text File
Display(); //Writes text file to display
}
private void btnSort_Click(object sender, EventArgs e)
{
Sort(); //Sorts list via BubleSort
Display(); //ReDisplays List
}
private void Sort() //Sorting Display List box
{
Account temp;
bool swap;
do
{
swap = false;
for( int x = 0; x < (accessList.Length - 1); x++)
{
if(accessList[x].ChildAccount.CompareTo( accessList[x+1].ChildAccount) > 0)
{
temp = accessList[x]; //Swapping if below
accessList[x] = accessList[x + 1];
accessList[x + 1] = temp;
swap = true;
}
}
}
while (swap == true);
}
}
}
> groups = new List
– xJoshii Nov 24 '19 at 13:12>(); List current = null;
foreach (var line in File.ReadAllLines(pathToFile))
{
if (line.Contains("CustomerEN") && current == null)
current = new List();
else if (line.Contains("CustomerCh") && current != null)
{
groups.Add(current);
current = null;
}
if (current != null)
current.Add(line);
}