Question Fill ListView With ArrayList from Text File

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am usually in the VB.Net forum..I am trying to convert to C# from VB.Net...

I am creating a program that the user can enter test results and save them to a text file...then they can fill the listview with the saved text file. I have the program loading an ArrayList with each line of the text file....one problem I see is the arraylist.....as it is right now it is only 5 lines long...the text file will get larger everytime the user saves the test results.

so I guess my question is , how would I get the text file contents into a list?

maybe use a List(of Integer) or List(of String)?

the text file looks like this

2/2/2013 5:14:36 PM
122
2223
223
555
this is a test Note!

here is the code I have

 private void frmViewResults_Load(object sender, EventArgs e)
        {
            // read the text file
            StreamReader sr = new StreamReader(Application.StartupPath + [URL="file://\\SugarData.txt""]\\SugarData.txt"[/URL], False);
            string strLine = "";
            // fill the ArrayList with the contents of the text file
            ArrayList Numbers = new ArrayList();
            while (strLine != null) 
            {
                strLine = sr.ReadLine();
                if (strLine != null)
                 
            Numbers.Add(strLine);
             
            }
            sr.Close();
            // fill variables from arrayList
            string date = Numbers[0].ToString();
            string firstNum = Numbers[1].ToString().Trim();
            string secondNum = Numbers[2].ToString();
            string thirdNum = Numbers[3].ToString();
            string fourthNum = Numbers[4].ToString();
            string note = Numbers[5].ToString();
            // fill ListView from variables
            ListViewItem lvi = new ListViewItem(date);
            lvi.SubItems.Add(firstNum);
            lvi.SubItems.Add(secondNum);
            lvi.SubItems.Add(thirdNum);
            lvi.SubItems.Add(fourthNum);
            lvi.SubItems.Add(note);
            LVResults.Items.Add(lvi);
        }


this works good...but if the text file gets larger , it doesnt......I know I need to add the text contents to something other than a ArrayList...but kinda lost now

Thanks for any Help

InkedGFX
 
Last edited:
Firstly, anyone targeting .NET 2.0 or later should not be using an ArrayList at all. It was superseded by the List(Of T) and that's what you should always use for dynamic array functionality.

As for your question, you should be using a generic List but not a List(Of String) or List(Of Integer). You should be using a List(Of ListViewItem). As you read the file, you create a new ListViewItem each time you encounter a new set of data. You add these to your List as you go and then, when you're done, you call AddRange once to add the whole lot to the ListView.
 
Thank You for your reply...do you have any examples of how I would go about this?

I have 5 columns in the listview ...
1 for the date the test results were created
1 for breakfast
1 for lunch
1 for dinner
and 1 for bedtime....

how would I fill the columns with the appropiate info for each column from the text file?

InkedGFX
 
Firstly, anyone targeting .NET 2.0 or later should not be using an ArrayList at all. It was superseded by the List(Of T) and that's what you should always use for dynamic array functionality.

As for your question, you should be using a generic List but not a List(Of String) or List(Of Integer). You should be using a List(Of ListViewItem). As you read the file, you create a new ListViewItem each time you encounter a new set of data. You add these to your List as you go and then, when you're done, you call AddRange once to add the whole lot to the ListView.

so as i read each line in the text file I need to add the line to the List<ListViewItem>() ?

Thanks for your help

InkedGFX
 
I added a List<string> Numbers = new List<string>()

I tried the List<ListViewItem> but couldn't get it to work......here is the code I have -

 private void frmViewResults_Load(object sender, EventArgs e)
        {
            // read the text file
            StreamReader sr = new StreamReader(Application.StartupPath + "[URL="file://\\SugarData.txt"]\\SugarData.txt[/URL]");
            string strLine = "";
            // fill the ArrayList with the contents of the text file
           // ArrayList Numbers = new ArrayList();
            List<string> Numbers = new List<string>();
            while (strLine != null) 
            {
                strLine = sr.ReadLine();
                if (strLine != null)
                   
                     Numbers.Add(strLine);
               
            }
            sr.Close();
           
            // fill variables from arrayList
           for (int i = 0; i < Numbers.Count; i++)
            {
               string date = Numbers[i].ToString();
                string firstNum = Numbers[i].ToString();
                string secondNum = Numbers[i].ToString();
                string thirdNum = Numbers[i].ToString();
                string fourthNum = Numbers[i].ToString();
                string note = Numbers[i].ToString();
                // fill ListView from variables
                ListViewItem lvi = new ListViewItem(date);
                lvi.SubItems.Add(firstNum);
                lvi.SubItems.Add(secondNum);
                lvi.SubItems.Add(thirdNum);
                lvi.SubItems.Add(fourthNum);
                lvi.SubItems.Add(note);
                LVResults.Items.Add(lvi);
            }
        }


where this kinda works...but it fills all six fields with the same info....for example

the first row in the list view will have the date in every column
then the next row will have the first test result in every column

ect....ect....


im getting closer ...I can feel it ....lol

thank you for your help

InkedGFX
 
well I think Im getting closer....the below code will fill the listview with each line of the text file .....but it only fills the first column with each line vertically....

   private void frmViewResults_Load(object sender, EventArgs e)
        {
            // read the text file
            StreamReader sr = new StreamReader(Application.StartupPath + "[URL="file://\\SugarData.txt"]\\SugarData.txt[/URL]");
            string strLine = "";
            // fill the ArrayList with the contents of the text file
          
            List<string> Numbers = new List<string>();
            while (strLine != null) 
            {
                strLine = sr.ReadLine();
                if (strLine != null)
                   
                     Numbers.Add(strLine);
               
            }
            sr.Close();
                // fill ListView with each line of the text file
                foreach (string line in Numbers)
                {
                    ListViewItem lvi = new ListViewItem(line);                 
                    LVResults.Items.Add(lvi);
                }
        }


not sure how to get this to work where it fills the columns horizontally....if that makes sence.

InkedGFX
 
One loop. List<ListViewItem>. If you're not going to follow my advice then I can't help. In the loop, read the appropriate number of lines from the file, create the ListViewItem with SubItems and add it to the List. When you're done, one call to AddRange.
 
would you have a sample code to get me started on what I need to do?

I try to implament the List<ListViewItem> and the loop but get to many errors to count........

InkedGFX
 
If you get too many errors to count then you're obviously trying to do too much at the same time. Break the problem down and build the solution one stage at a time. Don't move on to the next stage until you have the previous one working. So, show me your code to read the file and nothing else.
 
ok...here is the code I have to read the text file line by line....

  StreamReader sr = new StreamReader(Application.StartupPath + "[URL="file://\\SugarData.txt"]\\SugarData.txt[/URL]");
            
            string strLine = "";
           // code goes here for the List<ListViewItem>
            while (strLine != null) 
            {
                strLine = sr.ReadLine();
                if (strLine != null)
                // code goes here to add each line to the List
            }
            sr.Close();


Thank You for your help!

InkedGFX
 
I know the List<string> Numbers = new List<string>(); works because I run a MessageBox in a foreach loop and every line that is in the text file is shown in the messagebox.

I guess my problem is getting each line to be inserted correctly in the ListView on the form.

InkedGFX
 
So, how is the data formatted in the file? From your previous code is looks as though each set of six lines represents one item. Is that correct? A more usual approach would be to put all the data for one item on one line with delimiters between pairs of values, e.g. commas or Tabs.
 
Back
Top Bottom