|
-
Nov 6th, 2009, 05:35 PM
#1
Thread Starter
Junior Member
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
-
Nov 6th, 2009, 06:42 PM
#2
Re: Write listbox.items to a textfile
-
Nov 7th, 2009, 02:19 AM
#3
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:
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.
-
Nov 7th, 2009, 08:08 AM
#4
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
-
Nov 7th, 2009, 08:13 AM
#5
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?
-
Nov 7th, 2009, 06:47 PM
#6
Thread Starter
Junior Member
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...
-
Nov 7th, 2009, 07:25 PM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|