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:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,134
Location
Sydney, Australia
Programming Experience
10+
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.
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
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
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
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
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I think I would need a for loop with a counter for this?
am I going in the correct direction?

InkedGFX
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
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
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
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
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,134
Location
Sydney, Australia
Programming Experience
10+
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.
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
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
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,134
Location
Sydney, Australia
Programming Experience
10+
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.
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
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
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
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
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,134
Location
Sydney, Australia
Programming Experience
10+
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.
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
the data is fomatted like so.

Date
test result 1
test result 2
test result 3
test result 4
note ( if the end user adds a note...so this line may be empty)

would it make more sence to combine these six lines into 1 line then split it from there?

I would like to fill the listview with this info....I have a column for the date another column for test result 1 another column for test result 2 and so on....

thank you for your help.

InkedGFX
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,134
Location
Sydney, Australia
Programming Experience
10+
Yes it would make more sense to have that related data on a single line, as I already said in my previous post. The most common example of that is the comma-separated values (CSV) format, where records are delimited by line breaks and fields are delimited by commas. If you need to allow commas and/or line breaks within fields then you enclose those fields in double quotes. You can then read the file line by line and split each line into fields as you go. There's actually a class that does that in VB, i.e. the TextFieldParser. You can use it in C# too and it's a good idea to do so if you do have quoted fields but, in the simple case, you may a s well just use a StreamReader and call String.Split.
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
ok....so is there a way to fill the listview with the way I have it now?

I have scoured the internet to find examples of what I am trying to do , but I cannot find any.....

Thank You for your help.

InkedGFX
 

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
the below code is working , except it adds the same info in each row.....

   for (int i = 0; i < Numbers.Count; i++)
                {
                    ListViewItem lvi = new ListViewItem(Numbers[0]);
                    //  lvi.Text = Numbers[1];
                    lvi.SubItems.Add(Numbers[1]);
                    lvi.SubItems.Add(Numbers[2]);
                    lvi.SubItems.Add(Numbers[3]);
                    lvi.SubItems.Add(Numbers[4]);
                    lvi.SubItems.Add(Numbers[5]);
                    LVResults.Items.Add(lvi);
                   // MessageBox.Show(Numbers[i]);
                }


I use the MessageBox to show me that the list has the info I need in it.

InkedGFX
 
Top Bottom