Results 1 to 6 of 6

Thread: [Resolved] Listview Saving help! [VS2005]

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    uk
    Posts
    39

    Resolved [Resolved] Listview Saving help! [VS2005]

    Hi,
    i am trying to get it so i can save the information from a listview to a textfile but i am stuck on how to get the 2 columns 1 being time and the other one being Events to write to the text file. Can anyone please help me this is how far i have got so far.
    VB Code:
    1. saveFileDialog1 = new SaveFileDialog();
    2.             saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
    3.             saveFileDialog1.Title = "Save Text File";                        saveFileDialog1.ShowDialog();
    4.  
    5.             if (saveFileDialog1.FileName != "")
    6.             {
    7.                 System.IO.StreamWriter file = new System.IO.StreamWriter(saveFileDialog1.FileName);
    8.                 file.WriteLine(listview1.Items);
    9.                 file.Close();
    10.             }
    11.         }
    Last edited by sk|Lance; Oct 9th, 2006 at 02:17 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Listview Saving help! [VS2005]

    The Items property of a ListView is a collection of ListViewItems. The SubItems property of a ListViewItem is a collection of ListViewSubItems. Each subitem has a Text property. That's how you get the text displayed in a "cell" in a ListView. To visit every item in a collection you would use either a For or For Each loop. You have a collection of items and within each you have a collection of subitems. That means that you need two loops, one nested inside the other.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    uk
    Posts
    39

    Re: Listview Saving help! [VS2005]

    Hi,
    Ive tryed sorta of how you recommended however i am missing something here and cant get it to work for both columns or all rows could you possbily help me?

    VB Code:
    1. System.IO.StreamWriter file = new System.IO.StreamWrite(saveFileDialog1.FileName);
    2. for (int i = 0; i < this.lvLog.Items.Count; i++)
    3. {
    4. for (int j = 0; j < this.lvLog.Items[i].SubItems.Count; j++)
    5. {
    6. string tab = (j == 0) ? string.Empty : "\t";
    7. file.WriteLine(lvLog.Items[i].SubItems[j].Text);
    8. file.Close();
    9. }
    lvLog = the listview

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Listview Saving help! [VS2005]

    You are checking for the TAB but you are not writing it
    Code:
    string tab = (j == 0) ? string.Empty : "\t";
    file.WriteLine(lvLog.Items[i].SubItems[j].Text + tab);
    Another 2 things:
    1- you are not skipping a line when a row ends
    2- You are closing the file after each subitem is written
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    uk
    Posts
    39

    Re: Listview Saving help! [VS2005]

    i am not sure how to do the rest i am very new to c# and programming could you possibly help me? as for i am a noob and i cant currently think of how to do that.

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Listview Saving help! [VS2005]

    you are not skipping a line when a row ends
    put the following after the end of the for( j ) loop
    Code:
    file.WriteLine();
    You are closing the file after each subitem is written
    Move the
    Code:
    file.Close();
    to the end (when you stop writing data) after closing the for ( i ) loop
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width