Results 1 to 7 of 7

Thread: Write listbox.items to a textfile

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    28

    Smile Write listbox.items to a textfile

    This maybe a simple problem to VB gurus out here.

    I have a 4 listboxes, all 4 listboxes will be populated after I clicked the cmd_button. I need to have the items on
    all 4 listboxes written to a text (or preferably rtf file if possible)


    Private Sub

    Cmd button_click

    [INDENT]Listbox1.Add.Items ..........

    [INDENT]Listbox2.Add.Items .........

    Listbox2.Add.Items .........
    Listbox2.Add.Items .........

    End Sub


    The text file doesn't exist and must be created, it can be on a fixed path say C:/output file.txt, the file content sample below:

    -------------------------------------------------

    The content of the 1st box is:

    item 1
    item 2
    item 3


    The content of the 2nd box is:

    item 1
    item 2
    item 3

    The content of the 3rd box is:

    item 1
    item 2
    item 3

    The content of the 4th box is:

    item 1
    item 2
    item 3

    -------------------------------------------------
    Thanks in advance for the assistance -- Kathryn

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Write listbox.items to a textfile

    If you already know that it is a fairly simple thing that you want to do, then the easiest thing to do is do a search on google or on these forums - you will get answers much faster and learning how to find things out for yourself is something that you need to be able to do when learning to program. There are tons of tutorials on how to write to a text file, as well as lots of examples of how to loop through the items in a listbox, you just have to do a quick search for them If you try to do that but get stuck at a particular point then people here will gladly help you out but at least try yourself first

    With that said, here is an example to get you started:
    Code:
    For i As Integer = 0 To Listbox1.Items.Count - 1
         My.Computer.FileSystem.WriteAllText("C:\output.txt", Listbox1.Items(i).ToString & vbNewLine, True)
    Next
    Using a StreamWriter would be more efficient but for simplicity (and also to show that there is often more than one way to do something like this) I have just used the WriteAllText method. If you have any questions about how this code works then let me know and I'll explain.

    PS you can use the [CODE ] and [/CODE ] tags to make your example code look a bit neater and more readable like I have done in my example above
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    Re: Write listbox.items to a textfile

    Whenever you're dealing with doing the same thing to each of a collection of items, think LINQ:
    vb.net Code:
    1. IO.File.WriteAllLines("file path here", Me.ListBox1.Items.Cast(Of String)().ToArray())
    That assumes that the ListBox contains String objects in the first place.
    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

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Write listbox.items to a textfile

    I had no idea you could do something like that with LINQ - I thought it was just all about using SQL style statements to get data from collections... perhaps I should invest some time into learning a bit more about it
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Write listbox.items to a textfile

    Do you want all four written to the same text file or each to its own individual text file?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    28

    Thumbs up Re: Write listbox.items to a textfile

    I want all four listboxes saved to a single file, actually I need it to be Word doc.

    Here's what I have right now, seems to work though.

    HTML Code:
    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
    
            ' ***** This block will generate the summary report *******
    
            Dim Report As New System.IO.StreamWriter("C:\Report.doc")
    
            Report.WriteLine("*****REPORT *****")
    
            Report.WriteLine(vbNewLine)
            Report.WriteLine("Items in Listbox1")
            Report.WriteLine(vbNewLine)
    
    
            For Each currentItem As String In ListBox1.Items
    
                Report.WriteLine(currentItem)
    
            Next
    
    	  Report.WriteLine(vbNewLine)
    
    	  Report.WriteLine("Items in Listbox2")
    
            For Each currentItem As String In ListBox2.Items
    
                Report.WriteLine(currentItem)
    
            Next
    
           Report.Close()
    
           MsgBox("Report saved at C:\Report.doc")
    
        End Sub

    It works so I'm happy. The question is how can I format (Font as Bold, etc.) the Word doc. Another challenge (sigh)..

    Thanks for all the response...

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

    Re: Write listbox.items to a textfile

    I'm sorry to be the bearer of bad tidings but you are not saving a Word document there. You may have used a ".doc" extension but that doesn't make it a Word document. You can put a label that says "Sugar" on a jar that contains salt but that doesn't mean the jar now contains sugar. You're using a StreamWriter to save the file so it's a straight text file, no matter the extension you put on it.

    Creating a Word document is quite different to creating a text file. It's best to provide a full description of what you actually want up front to avoid irrelevant solutions. To save a Word document you will have to use Word Automation, which requires you to remote control an instance of the Word application. It's far more complex than saving a text file. If you can get away with it, I would suggest that you save an RTF file, which supports formatting like bold text but is far easier to create than an actual Word document. It can still be opened in Word but also in Windows WordPad and the like.
    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

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