简体   繁体   中英

Reading from multiple Text Files , arrays show on listbox and lables

Hi I have a program that:

1- user should first choose an item from a ComboBox .

Upon the selection, a text file is opened in the background and its contents added to a ListBox .

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    switch (comboBox2.SelectedIndex)
    {
        case 0:
            listBox3.Items.Clear();
            FileInfo file0 = new FileInfo("C:\\hardwaremaintenance.txt");
            StreamReader stRead0 =file0.OpenText();
            while (!stRead0.EndOfStream)
            {                
                listBox3.Items.Add(stRead0.ReadLine());
            }
            break;
        case 1:
            listBox3.Items.Clear();
            FileInfo file1 = new FileInfo("C:\\NetworkManagement.txt");
            StreamReader stRead1 =file1.OpenText();
            while (!stRead1.EndOfStream)
            {                
                listBox3.Items.Add(stRead1.ReadLine());
            }
            break;
        case 2:
            listBox3.Items.Clear();
            FileInfo file2 = new FileInfo("C:\\Software.txt");
            StreamReader stRead2 =file2.OpenText();
            while (!stRead2.EndOfStream)
            {                
                listBox3.Items.Add(stRead2.ReadLine());
            }
            break;
        case 3:
            listBox3.Items.Clear();
            FileInfo file3 = new FileInfo("C:\\SyriatelApplications.txt");
            StreamReader stRead3 =file3.OpenText();
            while (!stRead3.EndOfStream)
            {                
                listBox3.Items.Add(stRead3.ReadLine());
            }
            break;
        case 4:
            listBox3.Items.Clear();
            FileInfo file4 = new FileInfo("C:\\NewHardwareRequest.txt");
            StreamReader stRead4 =file4.OpenText();
            while (!stRead4.EndOfStream)
            {                
                listBox3.Items.Add(stRead4.ReadLine());
            }
            break;
    }
}

2-user select an item from the (recently) added items in the listbox (from step 1) and upon this action it again opened a new text file where its filled with a text from this way of format, where the " | " is the separation symbol

private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    int itemsCount = listBox3.Items.Count;
    string[] items = new string[itemsCount];

    for (int i = 0; i < itemsCount; i++)
        items[i] = listBox3.Items[i].ToString();

AND my brain suddenly stuck here.

The next step was supposed to take each item in the ListBox and connect it with the row from the last opened file WHERE the item selected == the first word in any of the rows.

What I don't know how to do it is:

  1. When and how to open the new file and read each row and place each item in an array (seperating rows from one another).
  2. How to compare the selected element from the ListBox with the fist word in any of the rows in the second file.

And if they match, I want to use the remaining info in the row to fill labels and textboxes.

The interface of the program as the following

I'm really sorry if I confused you, but I'm not that experienced in programming

A few tips to get you started:

  • don't use controls (listBox3) as data stores. Instead, add a string[] items to your class.
  • when you have repaeting code like in your switch() use Refactor|Extract method
  • TextFiles can be read a lot better and easier with System.IO.File.ReadAllLines(fileName)
  • When you want to find a word ( str=listbox3.SelectedItem ) in another file use lines[i].Contains(str)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM