0

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#.

Current Form

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);
        }
    }
}
xJoshii
  • 1
  • 2
  • You must first grab the string that you want to search and match agonist your text file. Similar question is asked here https://stackoverflow.com/questions/12856471/c-sharp-search-string-in-txt-file – S.N Nov 24 '19 at 12:42
  • Thanks for that, I'm quite new at programming so trying to understand how to work it into what I have is the issue. How would I go about doing that with: List> groups = new List>(); 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); } – xJoshii Nov 24 '19 at 13:12

0 Answers0